1

I've made a python program and i don't know why it doesn't work.

i tried this code:

import turtle
screen=turtle.Screen()
opened_door="C:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\opened_door.gif"
closed_door="C:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\closed_door.gif"
screen.addshape(opened_door)
screen.addshape(closed_door)
t=turtle.Turtle()
t.shape(closed_door)

but every time i ran the program it gave me errors like this:

  File "c:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\Monty Hall.py", line 3
    opened_door="C:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\opened_door.gif"
                                                                                ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

where is the mistake??

  • The existing response is correct, however for more information read this answer : https://stackoverflow.com/a/2953843/792015 – Inyoka Feb 14 '23 at 15:52

1 Answers1

1

The file paths contain backslashes, which is an escape character in Python. To get around this, you can prefix the strings with r:

opened_door=r"C:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\opened_door.gif"
closed_door=r"C:\Users\Roby&Gabry\Desktop\Monty Hall Paradox\closed_door.gif"
Kraigolas
  • 5,121
  • 3
  • 12
  • 37
  • 1
    Better yet, use pathlib or os.path to join the paths in an OS-agnostic way, and prefer relative rather than absolute paths, which fail if the project is moved. – ggorlen Feb 14 '23 at 17:24