Why is my program being interferred by comments?

Viewed 77

Our tasks at school is to add a test example of the program at the bottom of the code with:

#example

"""

<insert example of you running the code>

"""

but when I do so, I get error. I never get it on any other codes, only on this one?:

C:\Users\David>C:\Users\David\Desktop\IN1900\uke38\qerror.py 1
  File "C:\Users\David\Desktop\IN1900\uke38\qerror.py", line 22
    """
    ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 24-25: truncated \UXXXXXXXX escape

C:\Users\David>

My code is as following:

from math import sqrt
import sys

a=float(sys.argv[1])
b=float(sys.argv[2])
c=float(sys.argv[3])
d=(b**2)-(4*a*c)
x1=((-b+sqrt(d))/(2*a))
x2=((-b-sqrt(d))/(2*a))

print(f'The quadratic formula with used values gives two roots {x1:.1f} and {x2:.1f}')

#example
"""
#Fra command prompt:
#C:\Users\David>C:\Users\David\Desktop\IN1900\uke38\quadratic_roots_cml.py 1 0 -1
#The quadratic formula with used values gives two roots 1.0 and -1.0
#(koden funker ikke på et eller annet magisk vis når jeg bruker
"""
3 Answers

Part of your string literal is being interpreted as an escaped unicode character. Such escape sequences start with \U. The easiest fix for this is to put an r in front of the opening triplet of double quotes. This prevents any sort of escape processing of the text from happening:

r"""
#Fra command prompt:
#C:\Users\David>C:\Users\David\Desktop\IN1900\uke38\quadratic_roots_cml.py 1 0 -1
#The quadratic formula with used values gives two roots 1.0 and -1.0
#(koden funker ikke på et eller annet magisk vis når jeg bruker
"""

This is a lot easier than adding a bunch of extra slashes, and doesn't change the text's readability either.

Here's what the Python 3 docs say about "raw strings":

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, '\U' and '\u' escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the 'ur' syntax is not supported.

Use double blackslashes on the line with the path, like that:

"""
"""
#Fra command prompt:
#C:\\Users\\David>C:\\Users\\David\\Desktop\\IN1900\\uke38\\quadratic_roots_cml.py 1 0 -1
#The quadratic formula with used values gives two roots 1.0 and -1.0
#(koden funker ikke på et eller annet magisk vis når jeg bruker
"""
"""

The \U or \u in \Users and \uke38 is being interpreted as a Unicode escape. Double the backslash.

#C:\\Users\David>C:\\Users\David\Desktop\IN1900\\uke38\quadratic_roots_cml.py 1 0 -1

Alternatively, note that you have comments in lines starting with #. So, the triple-quotes are not needed.

...
print(f'The quadratic formula with used values gives two roots {x1:.1f} and {x2:.1f}')

#example
#Fra command prompt:
#C:\Users\David>C:\Users\David\Desktop\IN1900\uke38\quadratic_roots_cml.py 1 0 -1
#The quadratic formula with used values gives two roots 1.0 and -1.0
#(koden funker ikke på et eller annet magisk vis når jeg bruker
Related