简体   繁体   中英

Retrieving Dictionary Data Using raw_input

I am trying to implement a solution where I call the displayPerson() that takes the user input for an id number and will print the information for the user. I should note that I'm downloading a csv file from the internet that contains data in this format:

id, name , birthday
1, Jack Sparrow, 09/20/2000

My goal is to get a number from the user which will look up and display the ID. I want the prompt to continue to show up and ask the user for a number until they enter a negative number or 0 to exit.

 page = downloadData(args.url)
    persons = processData(page)


    prompt= int(raw_input(" Enter ID of person you would like to search for: "))

    displayPerson(prompt, persons)
    displayPerson(prompt, persons)

When I try to pass the raw input of a number 1-99 I get a Key Error, even though there are ids with that number. If I simply hard code displayPerson(10, persons) for example, the code will run but if I raw_input 10 I will get an error. Why?

Here is my displayPerson function:

def displayPerson(id, personDataDictionary):
    """

    :param id:
    :param personDataDictionary: Look at displayPerson in __name__ function. But I thought bday was pieces[2].
    :return:
    """
    print("id = {}, name = {}, birthday = {}".format(id, personDataDictionary[id][0],
                                                     personDataDictionary[id][1].strftime("%Y-%m-%d")))

As it is written I can call the function using the former code snippet (the first one you see up there) and the program will run fine if I enter an integer as one of the parameters manually but won't allow me to take a value from 1-99 without throwing a Key Error. How do I accomplish this?

Where is the dictionary that you are raising the Key Error on? Are the keys inputted as integers or strings? That could be one element you want to look into. I'm not 100% sure what you're asking for but if it's how to restrict the value of the var prompt to be between 1 and 99:

prompt = 0
while prompt < 1 or prompt > 99:
    prompt = int(raw_input(" Enter ID (1 - 99) of person you would like to search for: "))
do_something_with_key(prompt)

By the time you exit this loop, the value of prompt will be what you are looking for (unless what you're looking for is a string).

On my view,i suggest you to do this. Reason:why you throw a Key error because your dictionary did not have such key,so you should care about if you can get the value by key instead of setting range

def displayPerson(id, personDataDictionary):
    """

    :param id:
    :param personDataDictionary: Look at displayPerson in __name__ function. But I thought bday was pieces[2].
    :return:
    """
    per = personDataDictionary.get(id)
    while not per:
        prompt = int(raw_input(" Enter ID of person you would like to search for: "))
        per = personDataDictionary.get(prompt)

    print("id = {}, name = {}, birthday = {}".format(id, per[id][0],
                                                     per[id][1].strftime("%Y-%m-%d")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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