Python:如何使用列表作为用户输入的选择源? [英] Python: how to use list as source of selection for user input?
问题描述
任何人都可以检查此代码并让我知道出了什么问题吗?
Can anyone check this code and let me know what is wrong?
input_list = ["One", "Two", "Three"]
P1 = input("Select the input: ", input_list[0], input_list[1], input_list[2])
print (P1)
推荐答案
使用python的 raw_input
不可能将预选列表提供给用户以供选择.使用 raw_input
,我们可以收集原始字符串.
With python's raw_input
it is not possible to give a preselected list to the user to choose from. With raw_input
we collect raw strings.
更新:一个不错的解决方案是使用新的选择库: https://github.com/wong2/pick 它提供了一个小的curses界面,可以从给定列表中选择我们的选择.通过 pip install pick
进行获取.(更新:多项选择:是)
update: a nice solution is to use the new pick library: https://github.com/wong2/pick It provides a small curses interface to pick our choice from a given list. Get it with pip install pick
. (update: multi-select: yes)
更新2 :还有另一个python lib! https://curses-menu.readthedocs.org/en/latest/usage.html#getting-a-selection (无多选)
update 2: and yet another python lib ! https://curses-menu.readthedocs.org/en/latest/usage.html#getting-a-selection (no multi-select)
为此目的制作了一个小型且无需维护的库,选择器(多次选择:是).
There's a tiny and unmaintained library made for that purpose, picker (multi-select: yes).
我想到的最简单的解决方案是使用shell工具:
The simplest solution I'm thinking of is to use shell tools:
-
dialog
是Debian等发行版用于在控制台中呈现UI的东西, - selecta 是shell的模糊文本选择器,因此它完全符合我们的需求,除了红宝石工具,
-
zenity(和 yad-dialog )使构建起来非常容易Windows(我们退出终端).我可以显示以下列表:
dialog
is what distros like Debian use to present UIs in the console,- selecta is a fuzzy text selector for the shell, so it fits exactly our needs, except it is a ruby tool,
zenity (and yad-dialog) make it very easy to build simple windows (we exit the terminal). I can display a list with this:
zenity --list --text="a title" --column="first column" "first choice" "second choice"
我们还可以选择多个选择.
We can also select multiple choices.
这篇关于Python:如何使用列表作为用户输入的选择源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!