python - How do I make the program track user inputs at the loop code? -


i have created game called 'guessing game'. story is: computer generates number , user have 3 chances guess number computer generates. nice , easy code add small detail , thats got problem.

what asking guys me on how/what add code can associate new input previous 1 , calculate print response in accordance that.

import random  listte = range(1, 21) # list contains 1-20 number = random.choice(listte) # computer generated number list  in range(3):     answer = int(input("what guess?"))     prevanswer = none # came not working?     if answer == number:         print "oh yeah got it!"     elif answer < number:         print "make higher"     # elif not working below codes     elif answer < number , answer > prevanswer:         print "still higher"     elif answer > number:         print "lower!"     prevanswer = # not working came 

an example execution scenario:

computer generated : 15  guess 1 : 17 prints "lower!"  guess 2: 10 print "make higher"  guess 3: 12 print "still higher"  

extra: love if add code says "you have no more choice left" when user used of 3 guesses.

first, move setting of prevanswer = none before loop. otherwise, you're erasing memory of went before.

second, take @ if/elsif code sequence. have right tests, in wrong order:

elif answer < number: 

this execute every time answer less number. below have:

elif answer < number , answer > prevanswer: 

this "good" code, in sense should accomplish seem want. more restricted case earlier one. is, whenever answer < number, answer > prevanswer. should check subset of possibilities before general case of answer < number.

try this:

elif answer < number , answer > prevanswer:  # specific case     ... elif answer < number: # general case 

and finally, don't set

prevanswer = 

but rather

prevanswer = answer 

Comments