将用户输入作为字符串并打印变量 Python
我试图获取用户输入,然后将该输入转换为变量以打印出列表。
food_list = ["Rice", "l2", "l3"]
Rice = []
l2 = []
l3 = []
answer = input("What item would you like to see from the list")
if answer in food_list:
print("answer")
我希望输出是打印 Rice 列表,而不仅仅是像以前那样打印字符串“Rice”。输入将其视为字符串,但我想将输入转换为列表变量。
I was trying to take a user input and then convert that input into a variable to print out a list.
food_list = ["Rice", "l2", "l3"]
Rice = []
l2 = []
l3 = []
answer = input("What item would you like to see from the list")
if answer in food_list:
print("answer")
I wanted the output to be to print the Rice list, not just the string "Rice" like it has been. The input will take it as a string but I want to turn the input to the list variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以用字典来做到这一点:
You can do this with a dictionary:
Python 的
in
关键字功能强大,但它仅检查列表成员资格。你想要这样的东西:
快乐编码!
Python's
in
keyword s powerful, however it only checks list membership.You want something like this:
Happy coding!