-2
def fizz_buzz(i):
 if i % 15 == 0:
    return ("FizzBuzz")
 elif i % 5 == 0:
    return ("Buzz")
 elif i % 3 == 0:
    return ("Fizz")
 else:
    return (i)
for i in range(1, 21):
 print(fizz_buzz(i))

Where and how would do a new line command here with commas?

Trying to get an output like this: 1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,FizzBuzz,16,17,Fizz,19, Buzz
but sideways and with commas.

Nayuki
  • 17,911
  • 6
  • 53
  • 80
Gilgamesh
  • 29
  • 5

2 Answers2

0

Pass end=',' to print()

def fizz_buzz(i):
 if i % 15 == 0:
    return ("FizzBuzz")
 elif i % 5 == 0:
    return ("Buzz")
 elif i % 3 == 0:
    return ("Fizz")
 else:
    return (i)
for i in range(1, 21):
 print(fizz_buzz(i), end=',')

# prints
1,2,Fizz,4,Buzz,Fizz,7,8,Fizz,Buzz,11,Fizz,13,14,FizzBuzz,16,17,Fizz,19,Buzz,

If you do not want the trailing comma, end the range at 20 and follow with print(fizz_buzz(20))

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
0

Consider making a list & joining the elements with a comma. There are some great examples here.

For example:

def fizz_buzz(i):
    # your code

my_list = []
for i in range(1,21):
    my_list.append( str(fizz_buzz(i)) )
print ",".join(my_list)

There are more elegant ways of doing this -- using generators &c, as in the linked answer --, but this simple code will do what you want. Note that the join() method accepts string only, hence the str() in the list.append(); alternatively you could ensure that your fizz_buzz function returns strings regardless.

Community
  • 1
  • 1
Dean Ransevycz
  • 933
  • 8
  • 22