英文:
If a value of a user input is in a list, how can I use this input value in a new method?
问题
给定一个列表 ["a", "b", "c"]
接着是一个空列表 []
我希望用户输入一个值。
如果这个值出现在列表中,我希望该项从列表中跳到空列表。
到目前为止,我得到了这个:
列表 = ["a", "b", "c"]
空列表 = []
x = input("用户输入:")
if x in 列表:
空列表.append(列表.pop(列表.index(x)))
print
问题是:我如何定义用户输入,使其对应于列表中的正确索引位置?
我希望我表达清楚了。
英文:
Given a list ["a", "b", "c"]
followed by an empty list []
I want the user to input a value.
If this value appears on the list, I want the item to jump from the list to the empty one.
so far I got this:
list = ["a", "b", "c"]
empty_list = []
x = input("user_input: ")
if x in list:
empty_list.append(list.pop(*how do I define this?*))
print
the problem is: How do I define the input of the user such that it corresponds to the right index within the list?
I hope I've made myself clear.
I did not really try anything since I have no clue what to do. All I did was to look things up but so far no result.
答案1
得分: 0
欢迎来到SO,
你没有声明你正在使用的编程语言,但从我看到的情况来看,这是Python?
你需要找出该值的列表索引,在Python中,可以使用list.index(value)
。我为了更好的可读性,重命名了一些变量,但代码本质上是你的:
allowed_inputs = ["a", "b", "c"]
given_inputs = []
x = input("user_input: ")
if x in allowed_inputs:
given_inputs.append(allowed_inputs.pop(allowed_inputs.index(x)))
不过要注意,你的print
语句目前不会打印任何东西。我不确定你想要打印什么,是given_inputs
的当前内容吗?
如果是的话,你可能想使用print(given_inputs)
。
英文:
Welcome to SO,
you didn't declare the language you are programming in, but from what I see this is Python?
You will need to figure out the list index for the value, in Python it would be list.index(value)
. I renamed a few variables for better understandability but the code is otherwise yours:
allowed_inputs = ["a", "b", "c"]
given_inputs = []
x = input("user_input: ")
if x in allowed_inputs:
given_inputs.append(allowed_inputs.pop(allowed_inputs.index(x)))
print
Note however that your print
statement does not print anything at the moment. I am not sure what you would like to print, the current contents of the given_inputs
?
In that case you might want to use print(given_inputs)
instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论