-1
def fizz_buzz(nums):

    if nums % 5 == 0 and nums % 3 == 0:
       print("FizzBuzz")
       
    elif nums % 3 == 0:
        print("Fizz")

    elif nums % 5 == 0:
        print("Buzz")

    elif nums % 3 != 0 and nums % 5 != 0:
        print(nums)  

print(fizz_buzz(30))

Problems:

  1. Why None is appearing in the output?
  2. Can Anyone suggest what should I write in else statement? How I test else statement?

I am running this code in Google Colab.

3 Answers3

1

Try this:

def fizz_buzz(nums):
    if nums % 5 == 0 and nums % 3 == 0:
        return "FizzBuzz"
       
    elif nums % 3 == 0:
        return "Fizz"

    elif nums % 5 == 0:
        return "Buzz"

    elif nums % 3 != 0 and nums % 5 != 0:
        return nums
0

See, fizz_buzz(30) will return 30. But when you print the function itself, it's not worth anything. This is because the function doesn't return anything. A way to fix this would be to use the return keyword.

You could try this:

def fizz_buzz(nums):

    if nums % 5 == 0 and nums % 3 == 0:
       print("FizzBuzz")
       
    elif nums % 3 == 0:
        print("Fizz")

    elif nums % 5 == 0:
        print("Buzz")

    elif nums % 3 != 0 and nums % 5 != 0:
        print(nums)  

fizz_buzz(30)

Or

def fizz_buzz(nums):

    if nums % 5 == 0 and nums % 3 == 0:
       return "FizzBuzz"
       
    elif nums % 3 == 0:
        return "Fizz"

    elif nums % 5 == 0:
        return "Buzz"

    elif nums % 3 != 0 and nums % 5 != 0:
        return nums

fizz_buzz(30)
10 Rep
  • 2,217
  • 7
  • 19
  • 33
0

As you don't return anything from the function, Just call the function with a parameter. An example is given below.

def fizz_buzz(nums):
    if nums % 5 == 0 and nums % 3 == 0:
        print("FizzBuzz")

    elif nums % 3 == 0:
        print("Fizz")

    elif nums % 5 == 0:
        print("Buzz")

    elif nums % 3 != 0 and nums % 5 != 0:
        print(nums)

fizz_buzz(30)

Or you can return something and print outside of the function. An example is given below.

def fizz_buzz(nums):
    if nums % 5 == 0 and nums % 3 == 0:
        return "FizzBuzz"

    elif nums % 3 == 0:
        return "Fizz"

    elif nums % 5 == 0:
        return "Buzz"

    elif nums % 3 != 0 and nums % 5 != 0:
        return nums

print(fizz_buzz(30))
Shaiful Islam
  • 359
  • 2
  • 13