You vs. Computer Guess Game (with error handling and Save-into-file high score capability)
Error handling syntax
try:
except:
import random
rNum = random.randint(1, 100)
guesses = 0
while True:
try:
gn = int(input("We have chosen a number between 1 and 100. Guess what? "))
guesses += 1
if gn < rNum:
print("Hint: Choose a higer number")
if gn > rNum:
print("Hint: Choose a lower number")
if gn == rNum:
print(f"You guessed it right. Your number was {gn}")
print(f"Our chosen number was {rNum}")
print(f"You geussed it right in {guesses} guesses")
break
except:
print("invalid input. Plz enter only whole numbers!")
with open("hi-score2.txt", "r") as f:
hiscore = int(f.read())
if guesses < hiscore:
with open("hi-score2.txt", "w") as f:
f.write(str(guesses))
Comments
Post a Comment
Write something to CodeWithAbdur!