3

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 Answers3

2

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.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • It turns off much of the escape processing for the string literal that follows. See the following: https://docs.python.org/2.0/ref/strings.html - Note that the fact that it is a doc string has nothing to do with what's going on. Putting `r` in front of any other string literal has the same effect. – CryptoFool Sep 20 '20 at 16:52
  • I just realized that the link I supplied above is really old. Here's where you can read about `r` in the Python 3 docs: https://docs.python.org/3/reference/lexical_analysis.html - see above, where I added the relevant part of this page to my answer. – CryptoFool Sep 20 '20 at 16:59
1

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
"""
"""
Eliran Turgeman
  • 1,526
  • 2
  • 16
  • 34
1

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
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147