Accessing a local variable whose name comes from user input

前端 未结 1 429
南方客
南方客 2021-01-24 03:33

i need to access strings using raw_input.

list1 = [\"one\",\"Two\",\"three\"]

list2 = [\"1\",\"2\",\"3\"]

while True:

        ip = raw_input(\"en         


        
1条回答
  •  醉话见心
    2021-01-24 04:08

    Use a dict:

    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
    

    0 讨论(0)
提交回复
热议问题