I have this code running in a number guessing game I have written, it runs perfectly fine if the player follows the instructions, but as we all know users never do. If the user enters just a space or any string thats not the word hint then it crashes saying invalid literal for int() with base 10: when trying to convert the value of guess to an integer. Is there any way around this or am I just going to have to live with the crashes?
while repeat==1:
repeat=0
level1number=str(level1number)
guess=input("What is your guess? ")
guess=guess.lower()
if guess==level1number:
print("Well done, You have guessed my number!")
elif guess=="hint":
print("Hints are not available until level 3")
repeat=1
elif guess!=level1number:
print("Sorry that is not my number, you have lost a life. :(")
lives=lives-1
repeat=1
if lives<=0:
print("You have lost all your lives, so this means I win")
print("The program will now end")
exit()
input("")
level1number=int(level1number)
guess=int(guess)
if guess<level1number:
print("The target number is higher")
else:
print("The target number is lower")
whileloop andtryexceptblock to keep asking the user for input if it can't be converted toint.level1numbertostrandguessshould be an integer as soon as possible.