Skip to main content
 首页 » 编程设计

python之电话号码查询程序

2025年05月04日18shihaiming

我的数组作业有问题,我需要输入一个名称然后程序必须返回数字,但我所能做的就是返回所有这些

def main(): 
    people = ['todd','david','angela','steve','bob','josh','ben'] 
    phoneNumbers = ['234-7654','567-1234','888-8745','789-5489','009-7566','444-6990','911-9111'] 
 
    found = False 
    index = 0 
 
    searchValue = raw_input('Enter a name to search for phone number: ') 
 
    while found == False and index < len(people): 
        if people[index] == searchValue: 
            found = True 
        else: 
            index = index + 1 
 
    if found: 
        print 'the phone number is: ',phoneNumbers 
    else: 
        print 'that name was not found' 
 
main() 

请您参考如下方法:

使用 index 打印你想要的电话号码,而不是全部:

if found: 
    print 'the phone number is: ', phoneNumbers[index]