访问名称来自用户输入的局部变量 [英] Accessing a local variable whose name comes from user input
本文介绍了访问名称来自用户输入的局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用raw_input
访问字符串.
i need to access strings using raw_input
.
list1 = ["one","Two","three"]
list2 = ["1","2","3"]
while True:
ip = raw_input("enter list: ")
for i in ip:
print i
break
当"list1"作为输入给出时,它作为字符串而不是列表. 我需要访问上面定义的列表. 我需要一种访问列表并打印列表的方法.
When "list1" is given as input, it takes as string but not as list. I need to access the list defined above. I need a way to access the lists and print the list.
推荐答案
使用字典:
lists = {
"list1": ["one","Two","three"],
"list2": ["1","2","3"],
}
while True:
choice = raw_input("enter the list name: ")
try:
for item in lists[choice]:
print item
except KeyError:
print "I never heard of any list named '{}'! Try again.".format(choice)
else:
break
这篇关于访问名称来自用户输入的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文