Reputation: 1
i am new to python, i am currently reading Python crash course by Eric Matthes. In chapter 6 i am strugling with an exercice: We have a dictionary of friends(a, b, c, ..) and their favorite programing languages, we are supposed to check if the friends in the list "friends" have a favorite language(in the dictionary), if so, then print their name and favorite language, else, ask them what it is. It works, however when i print their favorite language it prints the whole set of values but i only want the one assigned to the name(key). It would be great if someone could explain how to do it, thanks.
favorite_languages = {
'a': 'python',
'b': 'java',
'c': 'ruby',
'd': 'python',
}
friends = ['a','b', 'c', 'd', 'e', 'f']
for friend in friends:
print(friend)
if friend in favorite_languages.keys():
print(f"\t{friend.title()}, Your favorite language is {favorite_languages.values()}!")
else:
print(f"\t{friend.title()}, What is your favorite language")
Upvotes: 0
Views: 130
Reputation: 13087
Rather than checking if our friend
is in the keys()
of favorite_languages
(which is technically possible), let's try to get the language of our friend or None using .get()
.
favorite_languages = {'a': 'python', 'b': 'java', 'c': 'ruby', 'd': 'python'}
friends = ['a','b', 'c', 'd', 'e', 'f']
for friend in friends:
## ------------------
## lookup our friend's language but if the key is not in the dictionary return None.
## ------------------
favorite_language = favorite_languages.get(friend)
## ------------------
if favorite_language:
print(f"\t{ friend }, Your favorite language is { favorite_language }!")
else:
print(f"\t{ friend }, What is your favorite language?")
This should give us:
a, Your favorite language is python!
b, Your favorite language is java!
c, Your favorite language is ruby!
d, Your favorite language is python!
e, What is your favorite language?
f, What is your favorite language?
Upvotes: 0
Reputation: 126
When looping with loop for ,you can use friend as index to get the proper value inside favorite_languages.I just corrected your code like that
favorite_languages = { 'a': 'python', 'b': 'java', 'c': 'ruby',
'd':
'python', }
friends = ['a','b', 'c', 'd', 'e', 'f']
for friend in friends:
if friend in favorite_languages.keys():
print(f"\t{friend.title()}, Your favorite language is
{favorite_languages[friend]}!")
else:
print(f"\t{friend.title()}, What is your favorite language")
Upvotes: 0
Reputation: 3174
favorite_languages.values()
returns a list with all the values, so ['python', 'java', 'ruby', 'python']
. But what you want is to look up in the dictionary what is the specific value for that key. The key is friend
, so you can look up the value for the key friend
in the dictionary. The if-clause is important here by the way, because if you were to look up a key that didnt exist using square brackets ([]
) you would get a KeyError because there is no value for this key.
favorite_languages = { 'a': 'python', 'b': 'java', 'c': 'ruby', 'd': 'python', }
friends = ['a','b', 'c', 'd', 'e', 'f']
for friend in friends:
if friend in favorite_languages.keys():
print(f"\t{friend.title()}, Your favorite language is {favorite_languages[friend]}!")
else:
print(f"\t{friend.title()}, What is your favorite language")
Upvotes: 1