Skip to content

Instantly share code, notes, and snippets.

@LeoAdamek
Created January 30, 2012 17:54
Show Gist options
  • Save LeoAdamek/1705669 to your computer and use it in GitHub Desktop.
Save LeoAdamek/1705669 to your computer and use it in GitHub Desktop.
Improved Number Game
# NumberGame.py - Improved Program
from random import randint
def get_guess():
proper_input = False
while not proper_input:
guess_input = raw_input('Enter a number between 1 and 100: ')
try:
guess = int(guess_input)
proper_input = True
except ValueError:
print "That is not a number, please enter a number"
return guess
if __name__ == '__main__': # Probably the hardest part of this program to explain
number_to_guess = randint(1,100)
guess = get_guess()
guesses = 1
while not guess == number_to_guess:
if guess > number_to_guess:
print "Too High."
else:
print "Too Low"
guesses += 1
guess = get_guess()
print "Congratulations, the number was ", number_to_guess
print "Well done, you guessed the number in ", guesses " guesses."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment