python - Variable value changes automatically -
i solved assignment # 2 course "an introduction interactive programming in python" @ coursera.org. assignment work ok according specifications. however, when user guess number correctly , game restarts, 1 chance automatically reduces. not happening when number of chances exhausted. logic both conditions same. can please explain , fix it.
here code:
# template "guess number" mini-project # input come buttons , input field # output game printed in console import simplegui import random # initialize global variables used in code secret_number=0 guess_counter=0 init_var = 1 # define event handlers control panel def range100(): # button changes range range [0,100) , restarts global secret_number, guess_counter, init_var secret_number = random.randrange(0, 100, 1) guess_counter = 7 print "" print "new game: range 0 100" print "number of guesses ", guess_counter #print secret_number def range1000(): # button changes range range [0,1000) , restarts global secret_number, guess_counter, init_var secret_number = random.randrange(0, 1000, 1) guess_counter = 10 init_var = 2 print "" print "new game: range 0 1000" print "number of guesses ", guess_counter #print secret_number def get_input(guess): # main game logic goes here global secret_number, guess_counter user_guess = int(guess) print "" print "guess was", user_guess if user_guess > secret_number: print "higher!" elif user_guess < secret_number: print "lower!" else: print "correct ... win!" initialize() guess_counter -= 1 if guess_counter == 0: print "you lost, guesses consumed" initialize() else: print "number of remaining guesses ", guess_counter def initialize(): #restart game, when guesses exhaust if init_var == 1: range100() else: range1000() # create frame frame = simplegui.create_frame("guess number", 300, 200) # register event handlers control elements frame.add_button("range [0, 100]", range100, 200) frame.add_button("range [0, 1000]", range1000, 200) frame.add_input("enter guess", get_input, 200) initialize() # start frame frame.start() # remember check completed program against grading rubric
add return under initialize()
elif user_guess < secret_number: print "lower!" else: print "correct ... win!" initialize() return
Comments
Post a Comment