电话簿的Python作业 [英] Python assignment for a phonebook

查看:162
本文介绍了电话簿的Python作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个星期的实验是基于Josh Cogliati(2005)的wikibook非程序员Python教程第53,54页的例子(见 http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3/字典)。



在他的例子中,Cogliati有打印,添加,删除和查找电话号码的选项。更改代码,以使字典中的值不是一个简单的电话号码,它现在是一个包含三个值的列表:




  • 电话号码

  • 电子邮件

  • 地址网页



关键还应该是人名。适应示例中相应的菜单,例如'2。添加电话号码'现在应该读为'2。添加条目,如果选择了,请向用户询问4项信息(姓名,电话,电子邮件,网络)。另外:
将一个选项(例如菜单中的第6个)添加到更改/编辑现有条目。
添加选项:




  • 打印电话号码列表

  • 打印仅列出电子邮件地址

  • 仅打印网址列表

  • 将所有上述内容打印在一起



这是我们给出的作业,我明白链接中给出了什么,并添加了一些,不确定如何添加一旦存储了电子邮件和网页信息的呼叫

解决方案

虽然我同意您的回答中的评论,我仍然会尝试我最好给你一些指导。



原始码:

  def print_menu():
print('1。打印电话号码')
print('2。添加电话号码')
print('3。删除电话号码')
打印('4。查找电话号码')
print('5。Quit')
print()

numbers = {}
menu_choice = 0
print_menu()
while menu_choice!= 5:
menu_choice = int(input(Type in a number(1-5):))
如果menu_choice == 1:
print(电话号码:)
for numbers.keys():
print(Name:,x,\tNumber:,numbers [x])
print()
elif menu_choice == 2:
print(Add Name and Number)
name = input(Name:)
phone = input(Number :)
number [name] = phone
elif menu_choice == 3:
print(删除名称和号码)
name = input(Name:)
如果数字中的数字:
del numbers [name]
else:
print(name,was not found)
elif menu_choice == 4:
print(Lookup Number)
name = input(Name:)
如果数字中的数字:
print(数字是,numbers [name])
else:
print(name,was not found)
elif menu_choice!= 5:
print_menu()

请注意,数字等于 {} - 这意味着它是一个字典,它存储键/值对。要添加到字典(或dict),您可以手动修改它: numbers = {'David':18003574689} 。因此,为了访问David的电话号码,您可以输入号码['David']



另一个添加它的方式是通过实例化(已经通过 numbers = {} 为您完成),然后向其中添加信息通过快捷方式公式 dictname ['key'] = value 。所以在这种情况下,速记可以是数字['Laura'] = 9173162546



现在,添加您可以使用列表,您可以使用 [] (这是python中的列表),但您可能会更好地将另一个字典嵌套到当前的一个。例如,您可以使用 numbers = {'David':18003574689} ,现在可以使用 numbers = {'David':{'phone number' :18003574689,'e-mail':'david2015@gmail.com','地址网页':'http://dave.com'},'劳拉':[...等...] code>。



要访问这些新的嵌套类型,您可以做的是简写数字['David'] ['phone数字'] ,这将返回他的#。然后,您可以再次执行这个确切的短码2次数字['David'] ['e-mail'] & 数字['David'] ['地址网页'] 。这三个人将访问相关的数据。



由于我相信这是一个新来的人最难的部分,所以我会停在这里,因为其余部分应该很简单。所有你需要做的是在条件下创建正确的的新输入。通过 = 赋值运算符(例如 email = input('Email:')),然后逻辑地使用其余的信息。我希望这有助于。


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).

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:

  • phone number
  • e-mail
  • 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:

  • 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

解决方案

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'].

Another way to add to it is by instantiating it (which is already done for you via numbers = {}), and then adding information into to it via the shortcut formula dictname['key'] = value. So in this case, the shorthand can be 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': 'david2015@gmail.com', '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.

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.

这篇关于电话簿的Python作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆