#!/usr/bin/env python # # a simple guessing game that uses Python's random # number generator to come up with a number to be # guessed by the user. # # version 2 - uses more functions and different # examples of users interacting with the # the program. # # written by e.bonakdarian oct 14, 2010 # "get/make accessible" the random number generator from random import randint # set lower and upper bounds on the number to be guessed # see how this propagates throughout the code LOW = 0 HIGH = 10 ############################################################ def play_game(): # play the game with guesses in the range LOW, HIGH target = randint(LOW, HIGH) # number to be guessed guess = input('Enter a number in range %d - %d: ' %(LOW, HIGH)) # get user's guess if guess == target: print 'You win!' else: print 'You lost :-) .. the number was %d' % target ############################################################ def main(): # start game(s) print 'We will play a simple guessing game.\n' play_game() answer = raw_input('\nDo you want to play again (y/n)? ') answer = answer.lower() # convert answer to lower case if answer == 'y': # boolean comparison play_game() elif answer == 'n': print 'Ok, sorry to see you go.' else: print '"%s" is an invalid response (y/n are valid)' % answer if __name__ == "__main__": main() print '\nQuitting program.', # note, comma prevents linefeed raw_input('Enter to exit ...')