python - How to restart a guess number game -
i using python 2.7.1
i able restart game based on user input, option of restarting, instead of exit game , open again. can me figure out??thanks
import random the_number = random.randrange(10)+1 tries = 0 valid = false while valid == false: guess = raw_input("\ntake guess: ") tries += 1 if guess.isdigit(): guess = int(guess) if guess >= 1 , guess <= 10: if guess < the_number: print "your guessed low" elif guess > the_number: print "your guessed high" elif tries <= 3 , guess == the_number: print "\n\t\tcongratulations!" print "you took",tries,"tries!" break else: print "\nyou guessed it! number was", the_number,"\n" print "you took",tries,"tries!" break else: print "sorry, between number 1 10" else: print "error,enter numeric guess" print "only between number 1 10" if tries == 3 , guess != the_number: print "\nyou didn't guess number in 3 tries, still can continue" continue while tries > 3 , guess != the_number: q = raw_input("\ngive up??\t(yes/no)") q = q.lower() if (q != "yes") , (q != "no"): print "only yes or no" continue if q != "no": print "the number is", the_number valid = true break else: break
put current code in function, such play_game or whatever. write loop invokes function until user has had enough. example:
def global_thermonuclear_war(): print "boom!" while raw_input("shall play game? [y|n] ") == 'y': global_thermonuclear_war()
Comments
Post a Comment