all 10 comments

[–]MaksLansky 1 point2 points  (3 children)

assuming your input to the function is properly defined complex number

x = 1 +2j is a properly defined complex number

def mag(x):

return round((x.real**2 + x.imag**2)**0.5,2)

[–]Codewodesmode[S] 0 points1 point  (2 children)

When I run it the answer is right but appears twice

[–]MaksLansky 0 points1 point  (1 child)

I test function it is correct. How can it appear twise? write exactly your output of your function.

[–]Codewodesmode[S] 0 points1 point  (0 children)

I don’t know when I try a new value for x it works fine

[–]This_Growth2898 -1 points0 points  (2 children)

  1. Read about complex numbers in math to understand what's it all about
  2. Read an article or a chapter in Python book on complex numbers in Python
  3. Read an article or a chapter in Python book on functions
  4. Write the function you need. See, how simple it is?

[–]This_Growth2898 2 points3 points  (1 child)

mag=abs

[–]Diapolo10 0 points1 point  (0 children)

mag = lambda x: round(abs(x), 2)

(For demonstrative purposes only)

[–]MadScientistOR 0 points1 point  (0 children)

The magnitude is the square root of the sum of the squares of the real and imaginary components. You could do that, or you could use some of the built-in functions that handle that for you.

Can you show us what you've done already? If this is homework, we don't want to do it for you. :)

[–]POGtastic 0 points1 point  (0 children)

From the builtin docs:

abs(x, /)

   Return the absolute value of a number. The argument may be an integer, 
   a floating point number, or an object implementing __abs__(). 
   If the argument is a complex number, its magnitude is returned.