0

我的作业问题是针对我在本网站上发布的上一个问题:

我将代码重做如下:

    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?是的,这是一项家庭作业。我在我的实际程序中更改了姓名和电话号码。

当我运行这个程序并输入任何字符时,所有的名字和电话号码都会出现,这就是我遇到的困难......

4

1 回答 1

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)

结果

在此处输入图像描述

于 2014-10-26T22:55:18.763 回答