How to print two different strings, depending on the lists' values

Hello, I recently started my crash course programming in python.

I’m working on chapter six, dictionaries and lists.

If you look at my code, how can I formulate my if- statement so that when a list only contains one value, the string will be printed in the singular form (?

Now, I have specified the names if friend == ‘jim’ or ‘tim’ print in the singular form.

print(“\n” + friend.title() + “'s favorite number is:”)

However, I want to know if there’s a more general statement so it’s not name depended, but value depended, or if it is possible at all.

Thanks in advance.

> # Dictionary of my friends and their favorite numbers
> 
> favorite_numbers = {'kim': [13, 48],
>                     'jim': [4],
>                     'wim': [55, 98, 3],
>                     'tim': [9],
>                     }
> for friend, numbers in favorite_numbers.items():
>     if friend == 'jim' or 'tim':
>         print("\n" + friend.title() + "'s favorite number is:")
>     else:
>         print("\n" + friend.title() + "'s favorite numbers are:")
>     for number in numbers:
>         print(number)
for friend, numbers in favorite_numbers.items():
    if len(numbers) == 1:
        noun = "number"
        verb = "is:"
    else:
        noun = "numbers"
        verb = "are:"
    print("\n" + friend.title() + "'s favorite", noun, verb)
    for number in numbers:
        print(number)

Keep in mind that there is a case that is not covered here. What if the friend has no favorite numbers?

1 Like

You’re right, I missed that one. Thanks for the help Alexander

That if is always true, which is not what you are expecting.
With () added to show how python runs the code it is this

If (friend == ‘jim’) or ‘tim’:

You can code it as


If (friend == ‘jim’) or (friend == ‘tim’):
2 Likes