1

I'm having an exercise where it told me to print out each person's favourite numbers. When it came to people with more than one number, I couldn't keep the numbers in one line, separated by commas. Can you suggest a solution?

My code

favourite_numbers = {
    "ha": [20, 10, 4],
    "quynh": [2, 5],
    "nhung": [3, 10, 12],
    "giang": [10],
}

for name, numbers in favourite_numbers.items():
    if len(numbers) == 1:
        for number in numbers:
            print(f"\n{name.title()}'s favourite number is {number}.")
    elif len(numbers) >= 2:
        print(f"\n{name.title()}'s favourite numbers are:")
        for number in numbers:
            print(f"{number}")

The output for a person with multiple numbers looks like this

Ha's favourite numbers are:
20
10
4

I was expecting it to be like this:

Ha's favourite numbers are: 20, 10, 4
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Hazel P
  • 17
  • 4

3 Answers3

1

Python String join() Method seems like it would be helpful.

favourite_numbers = {
    "ha": [20, 10, 4],
    "quynh": [2, 5],
    "nhung": [3, 10, 12],
    "giang": [10],
}

for name, numbers in favourite_numbers.items():
    if len(numbers) == 1: length = 'number is'
    elif len(numbers) > 1: length = 'numbers are'
    numbers = ', '.join(map(str, numbers))
    print(f"\n{name.title()}'s favourite {length}", numbers)

output:

Ha's favourite numbers are 20, 10, 4

Quynh's favourite numbers are 2, 5

Nhung's favourite numbers are 3, 10, 12

Giang's favourite number is 10
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
richard
  • 19
  • 2
0

You can do this by using two calls to print() when there is more than one number in the value list. In the second call to print() just unpack the list:

favourite_numbers = {
    "ha": [20, 10, 4],
    "quynh": [2, 5],
    "nhung": [3, 10, 12],
    "giang": [10],
}

for k, v in favourite_numbers.items():
    s = 's are' if len(v) > 1 else ' is'
    print(f"{k.title()}'s favourite number{s} ", end='')
    print(*v, sep=', ')

Optionally use map/join as follows:

for k, v in favourite_numbers.items():
    s = 's are' if len(v) > 1 else ' is'
    print(f"{k.title()}'s favourite number{s}", ', '.join(map(str, v)))

Output:

Ha's favourite numbers are 20, 10, 4
Quynh's favourite numbers are 2, 5
Nhung's favourite numbers are 3, 10, 12
Giang's favourite number is 10
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
-1
favourite_numbers = {
    "ha": [20, 10, 4],
    "quynh": [2, 5],
    "nhung": [3, 10, 12],
    "giang": [10]
}

for name in favourite_numbers:
    numbers_in_line = ""
    i = 1
    count = len(favourite_numbers[name])
    for number in favourite_numbers[name]:
        if i == count:
            comma = ""
            numbers_in_line += str(number) + comma
        else:
            comma = ", "
            numbers_in_line += str(number) + comma
        i += 1
    print(f"{name}'s favourite numbers are: {numbers_in_line}")

The output will be like that:

ha's favourite numbers are: 20, 10, 4
quynh's favourite numbers are: 2, 5
nhung's favourite numbers are: 3, 10, 12
giang's favourite numbers are: 10

Here is a print screen for the code after it was executed at PyCharm:

enter image description here

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
DevManX
  • 476
  • 3
  • 14
  • Your code does **not** produce that output – DarkKnight Jul 24 '23 at 06:44
  • It does, I checked it again twice, you just need to make sure the indentation is right, and the print occurs at the end of the first loop. I will add a print screen, to show the result. – DevManX Jul 24 '23 at 08:24
  • I appreciate your answer. Actually, the output you got isn't really what I wanted. There is one person who has only 1 favorite number, so the output should be "Giang's favourite number is" and not "[...] numbers are". – Hazel P Jul 24 '23 at 08:59
  • @YasserKhalil Your original answer did not produce the required output. Your current version does work albeit in a remarkably inefficient manner – DarkKnight Jul 24 '23 at 09:19