简体   繁体   English

在Python中仅接受数字作为输入

[英]Accepting only numbers as input in Python

Is there a way to accept only numbers in Python, say like using raw_input() ? 有没有办法只接受Python中的数字,比如使用raw_input()

I know I can always get the input and catch a ValueError exception, but I was interested in knowing whether there was someway I could force the prompt to accept only numbers and freeze on any other input. 我知道我总是可以得到输入并捕获一个ValueError异常,但我有兴趣知道是否有某些我可以强制提示只接受数字并冻结任何其他输入。

From the docs : 来自文档

How do I get a single keypress at a time? 我如何一次获得一个按键?

For Unix variants: There are several solutions. 对于Unix变体:有几种解决方案。 It's straightforward to do this using curses, but curses is a fairly large module to learn. 使用curses执行此操作非常简单,但curses是一个相当大的学习模块。 Here's a solution without curses: 这是一个没有curses的解决方案:

import termios, fcntl, sys, os
fd = sys.stdin.fileno()

oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)

oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

try:
    while 1:
        try:
            c = sys.stdin.read(1)
            print "Got character", `c`
        except IOError: pass
finally:
    termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)

You need the termios and the fcntl module for any of this to work, and I've only tried it on Linux, though it should work elsewhere. 你需要termios和fcntl模块来实现这一点,我只在Linux上试过它,尽管它应该在其他地方工作。 In this code, characters are read and printed one at a time. 在此代码中,一次读取和打印一个字符。

termios.tcsetattr() turns off stdin's echoing and disables canonical mode. termios.tcsetattr()关闭stdin的回显并禁用规范模式。 fcntl.fnctl() is used to obtain stdin's file descriptor flags and modify them for non-blocking mode. fcntl.fnctl()用于获取stdin的文件描述符标志,并将其修改为非阻塞模式。 Since reading stdin when it is empty results in an IOError, this error is caught and ignored. 由于在stdin为空时读取stdin会导致IOError,因此会捕获并忽略此错误。

Using this, you could grab the character, check if it's a number, and then display it. 使用此功能,您可以抓取角色,检查它是否为数字,然后显示它。 I haven't tried it myself, though. 不过,我自己没试过。

As far as I know, no. 据我所知,没有。 I've never heard of such a thing being possible with a terminal, in Python or any other language. 我从来没有听说过使用Python或任何其他语言的终端可能有这样的事情。

The closest way I can think of to fake it would be to put the terminal in silent mode (so that input characters are not echoed) and unbuffered mode (so that you get each character typed as it's typed, without waiting for the end of the line), then read each input character one by one; 我能想到的最接近假的方法是将终端设置为静默模式(以便输入字符不被回显)和无缓冲模式(这样你就可以输入每个字符的类型,而无需等待结束()),然后逐个读取每个输入字符; if it's a digit, print it and append it to a string, otherwise discard it. 如果它是一个数字,打印它并将其附加到字符串,否则丢弃它。 But I'm not even sure if the terminal would allow you to do that. 但我甚至不确定终端是否会允许你这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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