0

I just started programming. I watched a you tube tutorial on how to create a hangman game with pygame. When it comes to loading images I get this:

***FileNotFoundError**
Traceback (most recent call last)
<ipython-input-1-03373876a9dc> in <module>
     29 images = []
     30 for i in range(7):
---> 31     image = pygame.image.load("hangman" + str(i) + ".png")
     32     images.append(image)
     33 
FileNotFoundError: No such file or directory.*

The images (hangman 1...7)are located in the same folder as the ipynb for example the first one (C:\Users\basti\Documents\hangman\hangman1.png)

Can somebody help me with this? Thanks in advance :)

thomas
  • 1

1 Answers1

0

range(7) gives you an iterator from 0 to 6. If your images start at 1 and end at 7, you should use range(1,8)

mandulaj
  • 733
  • 3
  • 10
  • thanks for your help, yes forgot this, but unfortunately after correcting the code the same error appears again – thomas Jan 02 '21 at 23:46
  • Could you try printing `os.path.exists("hangman" + str(i) + ".png")` before the load, just to see, if python can see the path.... – mandulaj Jan 02 '21 at 23:52
  • it says "false". What does it mean that python can't see the path, I am confused? :D By the way thank you so much :) – thomas Jan 03 '21 at 00:10
  • 1
    Try using absolute path or perhaps building a path using os.path.join – illusionx Jan 03 '21 at 00:12
  • Probably the working directory of your jupyter notebook is set to somewhere weird. You can check it with `%pwd` and change it with `%cd` Try using the absolute path like this: `os.path.join("C:\Users\basti\Documents\hangman\", "hangman" + str(i) + ".png")` – mandulaj Jan 03 '21 at 00:17