Code:
while guess != num:
try:
guess = int(input("Guess: "))
if guess > num and guess < 101:
print("Too high, retry.")
tries += 1
elif guess < num and guess > 0:
print("Too low, retry.")
tries += 1
elif guess < 1 or guess > 100:
print("Don't be stupid, you have to type an integer between 1 and 100!")
except:
print("Are you crazy? Type an integer!")
"Catch all" except's are very bad. Suppose any other exception is thrown, for example the user presses Ctrl+C, you will still get "Type an integer". Also, try/except should only cover what is relevant. If an exception is thrown by anything after the first line, you will get "type an integer" also. This is correct:
Code:
while guess != num:
try:
guess = int(input("Guess: "))
except ValueError:
print("Are you crazy? Type an integer!")
continue
if guess > num and guess < 101:
print("Too high, retry.")
tries += 1
elif guess < num and guess > 0:
print("Too low, retry.")
tries += 1
elif guess < 1 or guess > 100:
print("Don't be stupid, you have to type an integer between 1 and 100!")
Bookmarks