I am building a math game similar to Zeta Mac and have nearly everything working perfectly. The only thing that I would like to change is for the next problem to show up right after the user inputs the correct answer, instead of them needing to press enter first.
If the answer is incorrect, the user should just use the backspace key to re-enter an answer. Which once correct should automatically trigger the next question.
In essence, I am trying to remove the need to use the enter key and only make the user use the numbers and the backspace key. Would that be feasible?
Here is the source code (sorry if it is messy, I just started learning python):
#import keyboard
import random
import time
import sys
def play(seconds):
start_time = time.time() # sets the start time to the current time
score = 0 # keeps track of the number of correct answers
while True:
current_time = time.time() # time at the nth iteration of the loop
elapsed_time = current_time - start_time # the difference between the time now and the time at the start
a = random.randint(2, 100)
b = random.randint(2, 100)
d = random.randint(2, 12)
asmd = random.choice([1, 2, 3, 4]) # choose between addition, subraction, multiplication, and division
if (asmd == 1):
solve = a + b
question = "%d + %d = " % (a, b)
elif (asmd == 2):
if (a > b):
solve = a - b
question = "%d - %d = " % (a, b)
else:
solve = b - a
question = "%d - %d = " % (b, a)
elif (asmd == 3):
solve = a * d
question = "%d * %d = " % (a, d)
else:
solve = a
c = a * d
question = "%d / %d = " % (c, d)
answer = input(question)
if(solve == int(answer)):
score += 1
# keyboard.press('enter') -- doesn't work
# results in ValueError: invalid literal for int() with base 10: ''
if elapsed_time > seconds:
print("Time\'s up! Your score was %d." % (score))
break
play(60)