#!/usr/bin/python
''' this is a number guessing game.the computer make a random number and store
into the memory.the player guess what the number is.the computer will give
the tips.
'''
from random import Random
import sys class GuessNumberGame: def __init__(self,player):
self.player = player
rd = Random()
self.number = rd.randint(0,100) def compare(self,guessNum): if guessNum > self.number: return 1 if guessNum < self.number: return -1 return 0 if __name__ == '__main__':
game = GuessNumberGame('jeff') print '''WOW!!!!let's start the game now.the computer now get a number in mind,the number is between 0 and 100,now what you guess?tell me by typing the number in your mind '''
count = 0 while True: try:
guess = int(raw_input('the number you guess =>')) except ValueError: print 'wrong value format! NUMBER is accept.' continue except EOFError: print 'good bye!'
count = count + 1
result = game.compare(guess) if result == 0: print 'grade!!you finally get the right number in %d times' % count break elif result == 1: print 'the number you guess is grade then the real number,please try again :)' elif result == -1: print 'the number you guess is small then the real number ,please try again :)' print 'Done!'