-1

I have a file isqrt.py, containing following code:

    from cmath import sqrt
    x = -1
    y = sqrt(x)
    print(y)

I am getting following error in my Mac Terminal:

File "isqrt.py", line 1
{\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf210
                                                  ^
SyntaxError: unexpected character after line continuation character

Do you know what is causing the error?

snowoman
  • 23
  • 4
  • It's a complex number that must be formatted to standar output. See this: http://stackoverflow.com/questions/7746143/formating-complex-numbers-in-python – Trimax Aug 08 '16 at 11:33

2 Answers2

4

Your error is showing you that the file you're running is not what you think it is; it's got a whole load of control characters. Seems like you've saved a file as RTF rather than plain text. Ideally, you should use a proper text editor to write Python code.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

Might be a bad idea to start with non-defined expressions, since the square-root of a negative number is not defined (at least when using real numbers).

What happens if you calculate the square-root of a positive number?

nostradamus
  • 712
  • 12
  • 24
  • The OP is using the `cmath` module, which works in complex numbers. The expected result is `1j` which is python's way of showing the imaginary unit i. – Rory Daulton Aug 08 '16 at 11:31
  • Should work with imaginary numbers as well, right. So, it's maybe indeed related to Daniel Roseman's suggestion? – nostradamus Aug 08 '16 at 11:32