显示python程序中存储值的输出

时间:2015-04-30 02:11:04

标签: python

我是python中的新学员。请帮我这个程序

name = ['a','b','c']
phone = [1,2,3]

我想输出一下,当我输入姓名' a',电话号码' 1'会显示等等。如果输出中没有找到存储在列表中的任何名称,则显示not found

请帮我怎么写这个程序!!

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码:

name = ['a','b','c']
phone = [1,2,3]
in_name = input("Enter a name: ") #gets the name
try:
    print(phone[name.index(input)]) # prints the phone number with same index as name
except:
    print("Value not found") # If there is no index(input) print value not found

但是,我不建议这样做。相反,使用字典:

phoneBook = {'a':1, 'b':2, 'c':3}

使用起来要容易得多。