how do i fix this? i'm not getting the right value for sarah

Question:

I am a beginner in python and I’m using python crash course book as my learning material.
I am currently working on the exercises with lists in a dictionary.

I am trying to do this example

and I cannot get it right. Here is my code below

favoriteLanguages = {
        'jen': ['python', 'ruby'],
        'sarah': ['c'],
        'edward': ['ruby', 'go'],
        'phil': ['python', 'haskell']
        }

for name, language in favoriteLanguages.items():
    if len(language) > 1:
        print(f"{name.title()}'s favorite languages are:")
        for l in language:
            print(f't{l.title()}')
    else:
        print(f"{name.title()}'s favorite language is {l.title()}.")

and this is the result that I get. I cannot get the value for sarah right

Asked By: Emman

||

Answers:

So you are doing wrong in the last line;

 print(f"{name.title()}'s favorite language is {l.title()}.")

you don’t ave to use l but language[0] as in this case, language will have only one element and you want to get that.

So the correct form will be;

 print(f"{name.title()}'s favorite language is {language[0].title()}.")

Also as you mentioned that you are a beginner, check how to use else statement with for loop as we can do that in python and you did that here by mistake.

Answered By: Irfan wani
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.