I am writing my own game about mental operations in your head such as addition, subtraction, multiplication and division. Right now, the game is running smoothly with addition and subtraction operations but what I want now is to ask to the user his/her guess but if he/she delays 5 seconds the input message should disappear, the correct result should appear and another operation should appear. This is the following code for the game:
def level_one():
goodGuess=0
badGuess=0
time_easy_level = default_timer()
numbers_with_operators=[]
local_operators_easy=["+","-"]
global continuePlaying
#====Repeat operations 5 times=================
for x in range(5):
#===10 random numbers between 1 and 10 are generated =========
easy_level=[randint(1,10) for i in range(1,10)]
#===Each list of random numbers is appended with a random operator===
for item in easy_level:
numbers_with_operators.append(item)
time.sleep(1)
numbers_with_operators.append(local_operators_easy[randint(0,1)])
if len(numbers_with_operators)==18:
numbers_with_operators.append(randint(1,10))
print numbers_with_operators
time_for_guess=time.time()
deadline_for_guess=time_for_guess+5
while time_for_guess<deadline_for_guess:
user_guess=int(raw_input("What is the result? "))
break
computer_result=compute_list(numbers_with_operators)
if user_guess==computer_result:
goodGuess+=1
print "Good guess!"
else:
print "Sorry, that is not the result"
badGuess+=1
print computer_result
del numbers_with_operators[:]
duration=default_timer()-time_easy_level
continuePlaying=False
print "Your results are: \n"
print "Good guesses: "+str(goodGuess)+"\nBad guesses: "+str(badGuess)
print "Total seconds playing:\n"+str(duration)+" seconds"
return continuePlaying
All suggestions are welcome and feel free to modify my code :)