Weird things happen when I try to run this piece of code:
with open("snake score.txt", mode="r") as f:
self.high_score = int(f.read())
The error I get is:
File "C:\Users\Lidor\PycharmProjects\100DaysOfCode\snake.py", line 77, in __init__
self.high_score = int(f.read())
ValueError: invalid literal for int() with base 10: ''
The output I get is '0'
CodePudding user response:
Make sure you have no extra spaces in your string.
self.high_score = int(f.read().strip())
Python can't read the file name and do stuff with the file if it has an empty whitespace/space character " " in it. Using the strip()
method, you remove the whitespaces from within the code and proceed.