# The import allows the program to access the pre-created code in the # library called 'random'. import random # guessingGame.py: # Author: James Tam # Version: February 4, 2008 # A simple game that randomly genrates a value between 1 and 10 and then # invites the user to guess the value that was generated. It illustrates # how a post-test loop works as well as how numbers are randomly generated. def guessingGame (): guess = 0 answer = 0 choice = "Y" print "" print "This program will randomly generate a number from 1 - 10." print "You have one chance to guess it then the program will tell" print "you if guessed correctly or not. It will continue generating" print "until you tell it to stop." print "" while choice not in ("q", "Q"): # The random number generator will return a value from 1 - 10. answer = random.randrange (10) + 1 guess = input("Enter your guess: ") if (guess == answer): print "You guessed correctly!" else: print "You guessed incorrectly" print "Number was", answer, ", your guess was", guess print "Play again? Enter 'q' to quit, anything else to play again" print "Choice:", choice = raw_input() print "" print "Exiting game"