1

I'm new to python (started about two weeks ago) and am trying to code my first python project; it is made to function like Zeta Mac arithmetic game (https://arithmetic.zetamac.com/)

This is a brief overview of how it works:

1.) Prompts the user with an Addition, Subtraction, Multiplication, or Division problem

2.) Asks the user to input an answer, then they must press enter

3.) If the answer is correct, the line is erased, the score is incremented and a new question is asked

4.) If the answer is wrong, it is erased and the user must input another guess

5.) The score is returned once time has run out

So far everything is working well, and I plan to learn how to use a gui to make it more similar to Zeta Mac

However there are two slight problems...

1.)

The first regards the timer. When the user is prompted to answer the question, "answer = input(question)" they can take as much time as they want, because even if the timer has run out, the loop has not finished until they input an answer, and only then can the final if statement be reached.

For example if you call "play(1)" which means you are playing the game for 1 second, you can sit there without typing anything for as long as you want because the last if statement that quits the program when time has run out is never reached.

2.)

The second one is also equally trivial, and I am not exactly sure if it is possible to fix. In Zeta Mac, when you input a correct answer, it is automatically read (no need for you to press the enter key), and the next question appears.

If you answer the question incorrectly nothing happens, and you must use the backspace/delete key to fix your answer, which when correct will trigger the next question.

Essentially I am trying to remove the need to use the enter key and rather have the user only touch the numbers (0-9) and the backspace/delete key. In this case I would also not need to use either of sys.stdout.write("\033[F") or sys.stdout.write("\033[K"). I am not sure if this is possible as I believe the console will only register input once the enter key is pressed?

Sorry if I am explaining this part poorly, if you try Zeta Mac it should make more sense.

Of course, both of these aren't huge issues, though I'd just like to know if it is possible for me to address them?

Thanks!

Here is my source code:

import random
import time
import sys

def play(seconds): 
    start_time = time.time()  # sets the start time to the current time
    score = 0 # counts 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) # Three random integers
        b = random.randint(2, 100)
        d = random.randint(2, 12)
        asmd = random.choice([1, 2, 3, 4]) # choose randomly between addition, subtraction, 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 = False
        while (solve != int(answer)): # while the answer is incorrect, clear the answer and allow user to try again
            answer = input(question)
            sys.stdout.write("\033[F") # clears the incorrect answer allowing the user to guess again
            sys.stdout.write("\033[K")
        score += 1 # increment score by 1
        
        if elapsed_time > seconds: # if the time has run up the loop ends
            print("Time\'s up! Your score was %d." % (score))
              

play(20)
SDC123
  • 45
  • 6
  • 1
    `input()` wasn't created to work with timer. It blocks code until you press `ENTER`. You may need to run it in separated `thread` and measure time in current thread and kill other thread when time is over. Eventually you can search module with function `getchar` which only check if key was pressed (and gives char or `None`) but it doesn't block code - so you can run it in loop which check if char was press (and keep it on some list) and check timer at the same time. This resolve also second problem because it doesn't wait for `ENTER` but in every loop you can check if have all chars on list. – furas Jun 04 '22 at 01:03
  • module may have name `getchar` or `getch` – furas Jun 04 '22 at 01:04
  • [python - Non-blocking console input? - Stack Overflow](https://stackoverflow.com/questions/2408560/non-blocking-console-input) – furas Jun 04 '22 at 01:06

0 Answers0