简体   繁体   中英

Python 3.4 How do you limit user input

I have made a program which requires the user to go and come back after completing the command, I would like the user to press ENTER to then continue the programme, to do this I used a normal Input command that does not record the answer. However I would like it if the user cannot enter any text, the only thing they can do is press ENTER to proceed.

input("Please charge your device, when you are finished, press ENTER on the keyboard")

You can try to write an if loop that checks whatever the input is enter: For example:

import tty
import sys
import termios

orig_settings = termios.tcgetattr(sys.stdin)

tty.setraw(sys.stdin)
x = input("Text")
while x != chr(13): # Enter
    x=sys.stdin.read(1)[0]
    print("ERROR PLEASE TRY AGAIN")
    x = input("Text")

termios.tcsetattr(sys.stdin, termios.TCSADRAIN, orig_settings) 

You might like to check this answer by @Turn at the question here: https://stackoverflow.com/a/34497639/2080764

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM