简体   繁体   English

我该怎么做呢? 通过用户输入向字典添加值?

[英]How do I do this? Adding values to the dictionary through user input?

I'm trying to write a program that uses a dictionary to create a personal phone book that keeps track of phone numbers, letting you access them by typing in the contact's name.我正在尝试编写一个程序,该程序使用字典创建一个个人电话簿来跟踪电话号码,让您通过输入联系人的姓名来访问它们。 The key for this dictionary is the name of the person whose phone number you want to retrieve.此字典的键是您要检索其电话号码的人的姓名。 The value is the phone number.该值是电话号码。 (Key and value are strings) (键和值是字符串)

I want to prompt the user to choose from the following menu options:我想提示用户从以下菜单选项中进行选择:

  1. Add new contact添加新联系人

  2. Query a contact查询联系人

  3. Exit出口

Options 1 and 2 should be implemented in a function -- meaning if the user chooses 1, the program execution should go to a function that performs the addition operation.选项 1 和 2 应该在 function 中实现——这意味着如果用户选择 1,程序执行应该 go 到执行加法操作的 function。

Option 1 should allow the user to enter as many contacts as they wish.选项 1 应允许用户输入任意数量的联系人。 Then it ends the loop on an empty string value for the key.然后它以键的空字符串值结束循环。 The function should print the phone book after the names are added. function 应在添加姓名后打印电话簿。

Option 2 prompts the user for a name and displays the corresponding value.选项 2 提示用户输入名称并显示相应的值。 The function should re-prompt the user if key is not found.如果未找到密钥,function 应重新提示用户。

I am most definitely stuck, here is my code so far:我绝对被卡住了,这是我到目前为止的代码:

pb = {"Isaiah":"6504213951", "Will":"7632991650", "Jack":"4319091874",
      "Tim":"2657554886", "Eric":"7837947361", "Joe":"7985579489",
      "Alex":"4616231562", "Steve":"8309117856"}

w = input("would you like to add a contact?")


#h = input("would you like to look up a contact?")
#name = input("state the name")

if w == "yes":
    add(w)

if w == "no":

    query(h)


def add(w):
    for key in pb:
        key = input("enter the key: ")
        value = input("enter the value: ")
    pb[key] = value  

    print(pb)

There has to be a more efficient way of doing this.必须有一种更有效的方法来做到这一点。 I'm trying to use if statements to call the function which has the dictionary addition code in it;我正在尝试使用if语句来调用其中包含字典添加代码的 function; but I don't know how to get the user prompts to access that code.但我不知道如何让用户提示访问该代码。

I want to prompt the user to choose from the following menu options我想提示用户从以下菜单选项中进行选择

So, make the prompt actually do this - show the menu, and then look for 1 , 2 or 3 in input, rather than yes or no .因此,让提示实际执行此操作 - 显示菜单,然后在输入中查找123 ,而不是yesno

Option 1 should allow the user to enter as many contacts as they wish.选项 1 应允许用户输入任意数量的联系人。 Then it ends the loop on an empty string value for the key.然后它以键的空字符串值结束循环。 The function should print the phone book after the names are added. function 应在添加姓名后打印电话簿。

Therefore, for key in pb: doesn't make sense here - that will loop for exactly as many times as there are existing entries in the phone book, instead of looking out for the empty string value.因此, for key in pb:在这里没有意义 - 这将循环与电话簿中现有条目完全相同的次数,而不是寻找空字符串值。

(Passing the input w to your add function also doesn't make sense - the function ignores it, and indeed it has nothing to do with performing the task.) (将输入w传递给您的add function 也没有意义 - function 会忽略它,实际上它与执行任务无关。)

I'm trying to use if statements to call the function which has the dictionary addition code in it;我正在尝试使用if语句来调用其中包含字典添加代码的 function; but I don't know how to get the user prompts to access that code.但我不知道如何让用户提示访问该代码。

I don't understand the question.我不明白这个问题。 your code calls add when a specific input is provided;当提供特定输入时,您的代码调用add add is the function that does what you want in this case (at least, once fixed up to loop properly). add是 function 在这种情况下可以执行您想要的操作(至少,一旦固定以正确循环)。 So isn't this already solved?那么这不是已经解决了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM