second game starts itself with wrong conditions in a python simle gui program -


i new python , trying make simple game using simplegui in python. in game, number guessed between either 0-100 or 0-1000 (depending upon user's choice) , user guess number. user gets maximum 7 attempts. new game starts after user lose or guess correct number. game should run continously.

problem: when first game finishes...second game , third game starts prints message you lose!.

my code following:

import simplegui import random import math   # initialize global variables used in code numofturns = 0  numberthought = 2000 maxallowedturns = 0 flag = 0 # 1: range (0, 100] , 2: range (0,1000]  # define event handlers control panel def range100():     # button changes range range [0,100) , restarts     global flag     flag = 1     new_game()  def range1000():     # button changes range range [0,1000) , restarts     global flag     flag = 2     new_game()  def input_guess(string_guess):     guess = int(string_guess)     global numberthought , numofturns, maxallowedturns, flag     numofturns = numofturns + 1      if ( numberthought > 1000 ):         print "please select range first"         print          return      # main game logic goes here      guessleft = maxallowedturns - numofturns     if(numberthought < guess):         print "your guess = ", guess         print "number of guesses remaining = ", (maxallowedturns - numofturns)         print "lower !"         print          if (guessleft == 0):             print "you lose!!"             print "the number = ", numberthought             print "<<<<<<<<<<<<<<<<<<<<<<<<",">>>>>>>>>>>>>>>>>>>>"             print             print             new_game()      elif (guess < numberthought):         print "your guess = ", guess         print "number of guesses remaining = ", (maxallowedturns - numofturns)         print "higher !"         print          if (guessleft == 0):             print "you lose!!"             print "the number = ", numberthought             print "<<<<<<<<<<<<<<<<<<<<<<<<",">>>>>>>>>>>>>>>>>>>>"             print             print             new_game()      elif (guess == numberthought):         print "your guess = ", guess         print "correct !"         print          new_game()         if (guessleft == 0):             print "you lose!!"             print "the number = ", numberthought             print "<<<<<<<<<<<<<<<<<<<<<<<<",">>>>>>>>>>>>>>>>>>>>"             print             print             new_game()                           # create frame frame = simplegui.create_frame("guess number", 300, 300) # register event handlers control elements frame.add_button("range (0,100]", range100) frame.add_button("range (0,1000]", range1000) frame.add_input("enter",input_guess, 100)  # call new_game , start frame # helper function start , restart game def new_game():     global numberthought , numofturns , flag, maxallowedturns     numofturns = 0     print "<<<<<<<<<<<<<<<<<<<<<<<<",">>>>>>>>>>>>>>>>>>>>"     print "new game !!!!"     # defining number of turns allowed based upon range type     if(flag == 1):         maxallowedturns = 7         numberthought = random.randrange(0,100)         print "range 0 - 100"         print "max allowed turns = " , maxallowedturns         print "number of guesses remaining = " , maxallowedturns-numofturns         print     elif(flag == 2):         maxallowedturns = 10         numberthought = random.randrange(0,1000)         print "range 0 - 1000"         print "max allowed turns = " , maxallowedturns         print  frame.start() 

(copy-pasted comment)

i looked @ code, didn't test (didn't want install simplegui), think prints lose when guess correct number using allowed turns. try removing if block "if (guessleft == 0): ... " in last elif block in input_guess.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -