电话簿的 Python 作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28910134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python assignment for a phonebook
提问by Kevin McGookin
This weeks lab is based on the example on pages 53,54 of the wikibook "Non-Programmers Tutorial For Python" by Josh Cogliati (2005), (see http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3/Dictionaries).
这周的实验基于 Josh Cogliati (2005) 的 wikibook“Non-Programmers Tutorial For Python”第 53,54 页上的示例(参见http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3 /字典)。
In his example, Cogliati has options for printing, adding, removing, and looking up a phone number. Change the code so that, instead of the value in the dictionary being a simple phone number, it is now a list with three values:
在他的示例中,Cogliati 可以选择打印、添加、删除和查找电话号码。更改代码,使字典中的值不再是一个简单的电话号码,而是一个包含三个值的列表:
- phone number
- address web page
- 电话号码
- 电子邮件
- 地址网页
The key should still be simply the persons name. Adapt the menu used in the example accordingly, for example the '2. Add a Phone Number' should now read '2. Add an entry' and if selected should ask the user for the 4 items of information (name, phone, email, web). Aditionally: Add an option (e.g. number 6 in the menu) to 'Change/Edit an existing entry'. Add options to:
关键仍然应该只是人名。相应地调整示例中使用的菜单,例如 '2. 添加电话号码”现在应改为“2。添加条目',如果选择,应询问用户 4 项信息(姓名、电话、电子邮件、网络)。另外:添加一个选项(例如菜单中的数字 6)以“更改/编辑现有条目”。添加选项:
- Print just a list of the phone numbers
- Print just a list of the e-mail addresses
- Print just a list of the web addresses
- Print all of the above together
- 只打印电话号码列表
- 仅打印电子邮件地址列表
- 仅打印网址列表
- 一起打印以上所有内容
This is the assignment we were given, I understand what's given in the link and have added a bit to it, unsure as to how to go about adding in the calling upon of the email and webpage information once stored
这是给我们的任务,我了解链接中给出的内容并添加了一些内容,不确定如何在存储后添加电子邮件和网页信息的调用
采纳答案by Hybrid
Although I agree with the comment under your answer, I will still try my best to give you some guidance.
虽然我同意你回答下的评论,但我仍然会尽力给你一些指导。
Original Code:
原始代码:
def print_menu():
print('1. Print Phone Numbers')
print('2. Add a Phone Number')
print('3. Remove a Phone Number')
print('4. Lookup a Phone Number')
print('5. Quit')
print()
numbers = {}
menu_choice = 0
print_menu()
while menu_choice != 5:
menu_choice = int(input("Type in a number (1-5): "))
if menu_choice == 1:
print("Telephone Numbers:")
for x in numbers.keys():
print("Name: ", x, "\tNumber:", numbers[x])
print()
elif menu_choice == 2:
print("Add Name and Number")
name = input("Name: ")
phone = input("Number: ")
numbers[name] = phone
elif menu_choice == 3:
print("Remove Name and Number")
name = input("Name: ")
if name in numbers:
del numbers[name]
else:
print(name, "was not found")
elif menu_choice == 4:
print("Lookup Number")
name = input("Name: ")
if name in numbers:
print("The number is", numbers[name])
else:
print(name, "was not found")
elif menu_choice != 5:
print_menu()
Notice that numbers
is equal to {}
- this signifies that it is a "Dictionary", which stores key/value pairs. To add to a dictionary (or "dict"), you can modify it manually as such: numbers = {'David': 18003574689}
. So, in order to access David's phone number, you would type in numbers['David']
.
请注意,numbers
它等于{}
- 这表示它是一个“字典”,用于存储键/值对。要添加到字典(或“字典”),您可以手动修改它是这样:numbers = {'David': 18003574689}
。因此,为了访问 David 的电话号码,您需要输入numbers['David']
.
Another way to add to it is by instantiating it (which is already done for you via numbers = {}
), and then addinginformation into to it via the shortcut formula dictname['key'] = value
. So in this case, the shorthand can be numbers['Laura'] = 9173162546
.
添加到它的另一种方法是实例化它(已经通过 为您完成numbers = {}
),然后通过快捷公式向其中添加信息dictname['key'] = value
。所以在这种情况下,简写可以是numbers['Laura'] = 9173162546
。
Now, to add a list
into the mix, you could use []
(which is a list in python), but you would probably be better suited nesting another dict into the current one. For example, instead of numbers = {'David': 18003574689}
, you can now have numbers = {'David': {'phone number': 18003574689, 'e-mail': '[email protected]', 'address web page': 'http://dave.com'}, 'Laura': [...etc...]}
.
现在,要将 a 添加list
到组合中,您可以使用[]
(这是 Python 中的列表),但您可能更适合将另一个 dict 嵌套到当前的 dict 中。例如,numbers = {'David': 18003574689}
您现在可以拥有numbers = {'David': {'phone number': 18003574689, 'e-mail': '[email protected]', 'address web page': 'http://dave.com'}, 'Laura': [...etc...]}
.
To access these new nested dicts, what you can do is the shorthand numbers['David']['phone number']
, which will return his #. You can then do this exact shortcode 2 more times numbers['David']['e-mail']
& numbers['David']['address web page']
. These three will access the associated data.
要访问这些新的嵌套字典,您可以做的是简写numbers['David']['phone number']
,它将返回他的 #。然后,您可以再次执行此确切的短代码 2 次numbers['David']['e-mail']
& numbers['David']['address web page']
。这三个将访问关联的数据。
Since I believe this is the toughest part for a newcomer, I'll stop here since the rest should be easy. All you have to do is create new inputs in the correct if
conditions. Assign the captured input data into proper variables via the =
assignment operator (ex. email = input('Email: ')
), and then use the rest of the info logically. I hope this helps.
因为我相信这对新手来说是最难的部分,所以我会停在这里,因为其余的应该很容易。您所要做的就是在正确的if
条件下创建新的输入。通过=
赋值运算符(例如email = input('Email: ')
)将捕获的输入数据分配给适当的变量,然后逻辑地使用其余信息。我希望这有帮助。