-2

I wrote the code for a fizz buzz game, where it prints fizz for multiples of 3 and buzz for multiples of 5. Then fizz buzz for both everything works but the output. Its supposed to print 1: 2: 3: fizz

and so on but mine prints an extra space

1 : 2 : 3 : fizz

I provided the code below thanks for any help

def fizzbuzz(n):
  for num in range(1,n+1):
    if num % 3 == 0 and num % 5 == 0:
      print(num,': FizzBuzz')
    elif num % 3 == 0:
      print(num,": Fizz")
    elif num % 5 ==0:
      print(num,": Buzz")
    else:
      print(num,":")




fizzbuzz(n=21)
Jonathan
  • 11
  • 4

1 Answers1

0

Probably f strings could work, but it only works for python 3.6 and above of it.

def fizzbuzz(n):
  for num in range(1,n+1):
    if num % 3 == 0 and num % 5 == 0:
      print(f'{num} FizzBuzz')
    elif num % 3 == 0:
      print(f'{num} Fizz')
    elif num % 5 ==0:
      print(f'{num} Buzz')
    else:
      print(f'{num}:')
      
fizzbuzz(n=21)
Brad Figueroa
  • 869
  • 6
  • 15