0

I had a look through the other answers but couldn't really find an answer to mine.

The program needs to return a persons age given their name.

    recordslist = [["Daniel", 18], ["John", 19], ["Paul", 20], ["Jack", 44]]

         def(name):



    checkAge(str(input("What is your name?")))

For example if Daniel is entered, the program returns 18. If John is entered, the program returns 19.

I'm not sure how to code this. There must be a for and in command somewhere in that def.

Thanks :-)

1
  • you're using the wrong data type. Use a dict instead of a list of lists, like: records = {"Daniel":18, "John":19"} then do records[name] Commented Apr 15, 2014 at 2:14

2 Answers 2

1

This is what you are looking for - dict(recordslist). A dictionary in Python is a key value pair. Your data can be better structured as a key value pair. Key being the name of the person and value being his/her corresponding score. dict(recordslist) converts your data from a list of lists to a dictionary.

You can access the value corresponding to a key in a dictionary as:

dictionary_variable[key]

Demo:

>>> recordslist = [["Daniel", 18], ["John", 19], ["Paul", 20], ["Jack", 44]]
>>> a_dict = dict(recordslist)
>>> a_dict
{'John': 19, 'Paul': 20, 'Daniel': 18, 'Jack': 44}
>>> a_dict['Daniel']
18

In your code you would do:

def checkAge(name, a_dict):
    if name in a_dict:
        return a_dict[name]
    else:
        return "No score found for {}".format(name)

result = checkAge(str(input("What is your name?")))
print result

Note: In order for this to work as you expect it to, you must have unique names in your list of list. This is what I mean:

>>> recordslist = [["Daniel", 18], ["John", 19], ["Paul", 20], ["Jack", 44], ["Daniel", 40]]
>>> a_dict = dict(recordslist)
>>> a_dict
{'John': 19, 'Paul': 20, 'Daniel': 40, 'Jack': 44}

I added ["Daniel", 40] to your recordslist and the a_dict has only one entry for Daniel.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, this answered things perfectly shak :-)
Hey Shak, I've just tried your code, and the program runs and asks for the name, but the program ends without displaying an age?
@DanielNitschke Are you calling the function and with the right parameters?
@DanielNitschke The function returns the result. Shouldn't you be either getting it's result in a variable and printing it OR printing the result in the checkAge method? Updated the answer to do so!
Heya, the old code should have just printed the return. I've adapted your new code, but am still receiving this parameter error: result = checkAge(str(input("What is your name?"))) TypeError: checkAge() missing 1 required positional argument: 'recordsdict'
|
0

As shaktimaan suggested, a dict is the right data structure if you have unique names.

If you have multiple entries with the same name potentially, you can use a list comprehension:

>>> recordslist = [["Daniel", 18], ["John", 19], ["Paul", 20], ["Daniel", 22]]
>>> tgt="Daniel"
>>> [(name, age) for name, age in recordslist if name==tgt]
[('Daniel', 18), ('Daniel', 22)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.