< prev | index | next >

Defensive Coding

from random import randint

while input('Play game? y/n: ').strip().lower() == 'y':
    answer = randint(1, 100)

    try:
        guess = int(input('Guess a number between 1 and 100: ').strip())
    except ValueError:
        print("That wasn't a number!")
        continue

    if guess == answer:
        print("You win!")
    else:
        print("You lose!")

< prev | index | next >