Python 如何让用户从有限列表中选择输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37565793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:34:26  来源:igfitidea点击:

How to let the user select an input from a finite list?

pythonmultiple-choice

提问by Gianni Alessandro

I'm quite new of python and programming, so I'm sorry if it's a "noob question". Anyway, is it possible to ask the selection between multiple choice in python, without a if loop?

我对python和编程很陌生,所以如果这是一个“菜鸟问题”,我很抱歉。无论如何,是否可以在没有 if 循环的情况下在 python 中进行多项选择?

Stupid example:

愚蠢的例子:

print "Do you want to enter the door"
raw_input ("Yes or not")

And the user can only choose between the selection.

而用户只能在选择之间进行选择。

回答by Khopa

If you need to do this on a regular basis, there is a convenient library for this purpose that may help you achieve better user experience easily : inquirer

如果您需要定期执行此操作,有一个方便的库可以帮助您轻松获得更好的用户体验:inquirer

Disclaimer : As far as i know, it won't work on Windows without some hacks.

免责声明:据我所知,如果没有一些黑客攻击,它将无法在 Windows 上运行。

You can install inquirer with pip :

您可以使用 pip 安装查询器:

pip install inquirer

Example 1 : Multiple choices

示例 1:多项选择

One of inquirer's feature is to let users select from a list with the keyboard arrows keys, not requiring them to write their answers. This way you can achieve better UX for your console application.

查询器的功能之一是让用户使用键盘箭头键从列表中进行选择,而不要求他们写下答案。通过这种方式,您可以为您的控制台应用程序实现更好的用户体验。

Here is an example taken from the documentation:

这是从文档中获取的示例:

import inquirer
questions = [
  inquirer.List('size',
                message="What size do you need?",
                choices=['Jumbo', 'Large', 'Standard', 'Medium', 'Small', 'Micro'],
            ),
]
answers = inquirer.prompt(questions)
print answers["size"]

Inquirer example

询问者示例

Example 2 : Yes/No questions :

示例 2:是/否问题:

For "Yes/No" questions such as yours, you can even use inquirer's Confirm :

对于像您这样的“是/否”问题,您甚至可以使用查询者的 Confirm :

import inquirer
confirm = {
    inquirer.Confirm('confirmed',
                     message="Do you want to enter the door ?" ,
                     default=True),
}
confirmation = inquirer.prompt(confirm)
print confirmation["confirmed"]

Yes no questions with Inquirer

是的,询问者没有问题

Others useful links :

其他有用的链接:

Inquirer's Github repo

Inquirer 的 Github 仓库

回答by holdenweb

One possible way to achieve what you appear to require is with a whileloop.

实现您似乎需要的一种可能方法是使用while循环。

print "Do you want to enter the door"
response = None
while response not in {"yes", "no"}:
    response = raw_input("Please enter yes or no: ")
# Now response is either "yes" or "no"

回答by toto_tico

For those in Python 3, and that want a non case sensitiveoption:

对于那些在Python 3 中,并且想要一个不区分大小写的选项的人:

def ask_user():
    print("Do you want to save?")
    response = ''
    while response.lower() not in {"yes", "no"}:
        response = input("Please enter yes or no: ")
    return response.lower() == "yes"

And, if I understand Assignment Expressions(PEP 572) correctly, in Python 3.8you will be able to do this:

而且,如果我正确理解赋值表达式PEP 572),在Python 3.8 中你将能够做到这一点:

def ask_user():
    while r:= input("Do you want to save? (Enter yes/no)").lower() not in {"yes", "no"}:
        pass
    return r.lower() == "yes"

回答by M.Vanderlee

For an OS agnostic solutions using prompt-toolkit 2 or 3, use questionary

对于使用提示工具包 2 或 3 的操作系统不可知解决方案,请使用 questionary

https://github.com/tmbo/questionary

https://github.com/tmbo/questionary