#!/usr/bin/env python #Guess the number! #Programmer: Michael P. Conlon, Ph.D. # Computer Science Department # 106 Maltby Center # Slippery Rock University # Slippery Rock, Pennsylvania 16057, U.S.A. # michael.conlon@sru.edu #Last modified February 7, 2002 #Copyright 2000, 2002, Michael P. Conlon #Permission is granted to use this program for any purpose, provided #the copyright notice(s) is/are maintained and attribution is given to #the author(s). from whrandom import * from string import * import sys ro = whrandom() #Create random-number object. play_again = 1 while play_again: number = ro.randint(1, 100) print "I am thinking of a number between 1 and 100." print "You have 10 chances.\n" guess_count = 0 while guess_count < 10: guess = -1 while guess < 1 or guess > 100: if guess_count == 0: guess = raw_input("Guess the number: ") else: guess = raw_input("Guess again: ") try: guess = int(guess) except ValueError: print "Entry must be a whole number between 1 and 100.\n" guess_count = guess_count + 1 if guess > number: print "Too high!\n" elif guess < number: print "Too low!\n" else: print "You guessed it! It took you %d guesses.\n" % guess_count break else: print "You have run out of guesses. The number was %d.\n\n" % number again = raw_input("Would you like to play 'Guess the number?' again? ") play_again = (again[0] == 'y') or (again[0] == 'Y') print "Goodbye!"