Python High Low Game Reversed Optimized -
def highlowr(): play_again = 1 while play_again==1: ct = 0 guess = 50 x = 0 ans = "" print "lets play game. \nthink of number between 1 , 100.\n" while ans!="c": temp0 = 0 temp1 = 0 print "i guess %d" %guess ans = raw_input("am (h)igh, (l)ow, or (c)orrect? \n") if ans=="h": temp0 = guess/2 temp1 = guess%2 guess = temp0 + temp1 elif ans=="l": temp0 = guess/2 temp1 = guess%2 guess = guess + temp0 + temp1 elif ans=="c": print "i got it! took me %d guesses." %ct else: print "i didn't quite understand meant there." ct = ct+1 play_again = input("would play again? yes = 1, no = 0: ") print"" print "thanks playing!" highlowr()
i have running reverse high low game cant figure out how change math in if statements optimize results. works if number guessed 1... cant figure out can optimize results. help?
i don't know you're trying temp0 , temp1. better keep min , max in memory. should work:
def highlowr(): play_again = 1 while play_again==1: ct = 0 max = 10 min = 0 guess = 0 x = 0 ans = "" print "lets play game. \nthink of number between 1 , %d.\n"%max while ans!="c": if (max+min)/2 == guess: print "i think lied me @ point. answer %d" %guess break guess = (max+min)/2 print "i guess %d" %guess ans = raw_input("am (h)igh, (l)ow, or (c)orrect? \n") if ans=="h": max = guess elif ans=="l": min = guess elif ans=="c": print "i got it! took me %d guesses." %ct else: print "i didn't quite understand meant there." ct = ct+1 play_again = input("would play again? yes = 1, no = 0: ") print"" print "thanks playing!" highlowr()
Comments
Post a Comment