1

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)
SDC123
  • 45
  • 6

1 Answers1

0

What you need your code to do is get the user entered value, check if it's correct and make a decision. In pseudo code:

while time_not_up
    answer = user_input
    if solve == answer
        next_problem
    else
      sleep 5 ms

AFAIK, this isn't feasible with a console app, since input isn't read until the user presses enter.

TBaggins
  • 62
  • 5
  • Yeah so as of now, I have two issues. The first is with the enter button, could something like this work (https://stackoverflow.com/questions/3523174/raw-input-without-pressing-enter)? The second is with the time, which works in counting, but the program does not print time is up while the user is being prompted for the answer (answer = input(question) even if there is no time left. This is because the if statement that checks time and quits the function is not reached yet. Thanks! – SDC123 Jun 03 '22 at 17:22