2 guess numbers

来源:互联网 发布:world of goo mac 编辑:程序博客网 时间:2024/06/03 02:50



# template for "Guess the number" mini-project# input will come from buttons and an input field# all output for the game will be printed in the consoleimport simpleguiimport randomtimes=7num_range=100# helper function to start and restart the gamedef new_game():    # initialize global variables used in your code here    global secret_number     global n    n=times    secret_number = random.randrange(0, num_range)    print "New game. Range is from 0 to "+str(num_range)    print "Number of remaining guesses is "+str(n)    print ""# define event handlers for control paneldef range100():    # button that changes the range to [0,100) and starts a new game     global num_range    num_range=100    global times    times=7         new_game()        def range1000():    # button that changes the range to [0,1000) and starts a new game         global num_range    num_range=1000      global times    times=10     new_game()                def input_guess(guess):    # main game logic goes here          global n    n=n-1    print "Guess was "+str(guess)    print "Number of remaining guess is "+str(n)    guess=int(guess)    if secret_number>guess:        print "Higer"        print ""            elif secret_number<guess:        print "Lower"        print ""            else:        print "Correct"        print ""        new_game()        if n==0:        print "You ran out of guesses. The number was "+str(secret_number)                     # create frameframe = simplegui.create_frame('Guess the number',300,300)inp = frame.add_input('Enter a guess', input_guess, 50)# register event handlers for control elements and start frameframe.add_button("Range: 0 - 100", range100, 100)frame.add_button("Range: 0 - 1000", range1000, 100)# call new_game new_game()# always remember to check your completed program against the grading rubric


0 0
原创粉丝点击