Python Forum
How to recognize space or enter as one-character input?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to recognize space or enter as one-character input?
#1
The program I'm currently working on asks the user to press SPACE if correct or ENTER if incorrect. Can you give me a code snippet that will enable Python to recognize this? I started with this:
checkif = True #this indicates whether I still need to check input
for r in range(origlistsize): #this will be done origlistsize times (total # of problems in list)
    question = random.randint(0,curlistsize)
    answer = input(multfacts[question])
    while checkif == True:
        if answer=="" or answer==" ": checkif = False
        else:
            answer = input("Enter SPACE if that was correct or ENTER if wrong: ")
The problem is that in order to recognize a space or enter, the enter key has to be subsequently pressed. This makes for two keystrokes when I really want one due to time constraints.
Reply
#2
The input function returns what the user typed up to but not including the final enter. So if they just hit enter, and empty string ('') will be returned. If they hit space + enter, a space character will be returned (' ').

If they just hit space without hitting enter, input will sit there waiting for them to hit enter.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Oct-17-2019, 03:44 PM)ichabod801 Wrote: The input function returns what the user typed up to but not including the final enter. So if they just hit enter, and empty string ('') will be returned. If they hit space + enter, a space character will be returned (' ').

If they just hit space without hitting enter, input will sit there waiting for them to hit enter.

Is there an altogether different command that solicits input and records it without the "final enter?"

I've seen programs in the past (language unknown) that will say things like "enter Y or N" and proceed once that first keystroke (Y or N) is pressed. That's what I'm trying to do.
Reply
#4
(Oct-17-2019, 05:06 PM)Mark17 Wrote: Is there an altogether different command that solicits input and records it without the "final enter?"

You can build one, but that low level access to the system makes it platform dependent. There's a thread at Stack Overflow with some examples.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks!
Reply
#6
There is a library called keyboard that may help. Looks like works on Windows and Linux, not sure about Mac.
Keyboard module on PyPI
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python rule about the space character surrounding the equal sign ineuw 10 1,519 Sep-21-2023, 09:17 AM
Last Post: ineuw
  python cant recognize PIP siubikYT 2 709 Jul-19-2023, 06:30 PM
Last Post: Lahearle
  How to split the input taken from user into a single character? mHosseinDS86 3 1,138 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Is it possible to make a program recognize how many clicks it has had on the monitor? jao 0 1,129 Feb-25-2022, 06:31 PM
Last Post: jao
  My program won't recognize the filename. braingoblins 1 1,096 Jan-07-2022, 06:18 PM
Last Post: deanhystad
  [solved] unexpected character after line continuation character paul18fr 4 3,296 Jun-22-2021, 03:22 PM
Last Post: deanhystad
Star NameError – function doesn't recognize imported modules Sir 4 3,419 Dec-01-2020, 06:36 AM
Last Post: Sir
  from global space to local space Skaperen 4 2,269 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,110 Jul-13-2020, 07:05 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,675 Jan-05-2020, 11:50 AM
Last Post: vivekagrey

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020