Is there a way prompt user for input and continue after set number of characters, so as to avoid waiting for the enter key in python 3?

Question:

I am trying to write a python typing test that displays 5 letter words from a list in a timed while loop of 60 seconds. Everything works if the user presses enter after each typed word but this allows for backspacing and isn’t my goal. Is input() able to move on after 5 keys have been typed (including arrow keys and backspace key) without the enter key press? Just moving on to the next word as if enter was pressed?

import time
import random

                           
t_end = time.time() + 60    # Set timer for 60 seconds


correct = 0
incorrect = 0
wpm_count = 0
accuracy = 0

words=["buggy", "coder", "etc.."]    #TODO: put lots of 5-character words in a list

while time.time() < t_end:
    for word in random.sample(words, 1):
        print(word, end=" ")
        wpm_count += 1
        user_txt = input("  ")
        if (user_txt == word):
            correct += 1
            
        else:
            incorrect += 1

#Calculate and display results
accuracy = (correct / attempts ) * 100
print("=-=-=- Results: -=-=-= nCorrect: " + str(correct) + " nErrors: " + str(incorrect) + " n Total WPM: " + str(attempts) + "nAccuracy: " + str(accuracy) )

            
            
Asked By: Slm Coding

||

Answers:

Yes, you can use the getch() function from the curses module to read keyboard input without requiring the user to press Enter after each word. Here’s an example implementation of your code using getch():

import time
import random
import curses

t_end = time.time() + 60    # Set timer for 60 seconds

correct = 0
incorrect = 0
attempts = 0
accuracy = 0

words=["buggy", "Help!", "etc.."]    #TODO: put lots of 5 character words a list

# initialize the curses module
stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(True)

# loop until the timer runs out
while time.time() < t_end:
    for word in random.sample(words, 1):
        stdscr.addstr(word + " ")
        stdscr.refresh()
        
        # read input without requiring Enter
        user_txt = ""
        while len(user_txt) < len(word):
            c = stdscr.getch()
            if c == curses.KEY_BACKSPACE:
                user_txt = user_txt[:-1]  # remove last character
            elif c >= 32 and c <= 126:  # printable characters
                user_txt += chr(c)
            elif c == 27:  # escape key
                break
        
        # compare input to expected word
        attempts += 1
        if user_txt == word:
            correct += 1
        else:
            incorrect += 1

# cleanup curses module
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()

# calculate and display results
accuracy = (correct / attempts ) * 100
print("=-=-=- Results: -=-=-= nCorrect: " + str(correct) + " nErrors: " + str(incorrect) + " n Total WPM: " + str(attempts) + "nAccuracy: " + str(accuracy) )
Answered By: Yasha_ops
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.