搜索姓名和电话号码

时间:2014-10-26 18:57:56

标签: python-3.x parallel-arrays

我的回答是我在本网站上发布的上一个问题:

我将代码重新编写为以下内容:

    import re
    people = ["Karen", "Peter", "Joan", "Joe", "Carmen", "Nancy", "Kevin"]

    phonenumbers = ["201-222-2222", "201-555-1212", "201-967-1490", 201-333-3333",'201-725-3444", "201-555-1222", "201-444-4656"]

name = raw_input("Enter person's name:")

found = false
for i in range(0, len(people)):

   value = people[i]
   m = ("(" + name + ".*)",value)

   if m:
       found = True
       print (people[i], phonenumber[i])
   else:
       print ("No matching name was found.")

我的问题是如何告诉程序检查Karen的电话号码是否为201-222-2222?是的,这是一个家庭作业。我在我的实际计划中更改了姓名和电话号码。

当我运行此程序并输入任何字符时,所有姓名和电话号码都会显示我所处的情况......

1 个答案:

答案 0 :(得分:0)

已编辑:问题不明确。

以下代码我的帮助。

1。)首先询问姓名,然后检查人员名单中是否存在 2.)然后如果存在则将其保存在名为abc的变量中 3.)循环结束后,打印出abc,即您输入的名称和该人员的电话号码。

import re
people = ["Karen", "Peter", "Joan", "Joe", "Carmen", "Nancy", "Kevin"]

phonenumbers = ["201-222-2222", "201-555-1212", "201-967-1490", "201-333-3333","201-725-3444", "201-555-1222", "201-444-4656"]

name = input("Enter person's name:")
abc = "" # Will store the name and phone number
found = False 
for i in range(0, len(people)):
   if people[i].lower() == name.lower(): #checks if input name match + use method to lower all char
          abc = people[i]+" Has the number "+phonenumbers[i]
   value = people[i]
   m = ("(" + name + ".*)",value)
   if m:
       found = True
       print (people[i], phonenumbers[i]) # missing letter "s"
   else:
       print ("No matching name was found.")

print("\n"+abc)

结果

enter image description here