Search code examples
pythonfunctionfizzbuzz

Beginner creating fizzbuzz function


This code is supposed to output fizz if number is divisible by 3, buzz if divisible by 5 and fizzbuzz if divisible by 3 and 5. Although I'm a bit unfamiliar with defining my own function and using the return appropriately. How do I remove the last 16 if the user inputs the number 16?

number = int(input("Enter a number: "))


def fizzbuzz(number):
    n = 1
    while n <= number:
        if n % 3 != 0 and n % 5 != 0:
            print(n)
        elif n % 3 == 0 and n % 5 == 0:
            print("fizzbuzz")
        elif n % 3 == 0:
            print("fizz")
        elif n % 5 == 0:
            print("buzz")
        n = n + 1
    return number

print(fizzbuzz(number))

If number = 16 it outputs

Enter a number: 16
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
16

How do I get I remove the last number 16, as it isn't supposed to be there


Solution

  •             print("buzz")
            n = n + 1
        return number
    
    print(fizzbuzz(number))
    

    Here's your problem. Don't return the number, and don't print the return value of the function.

                print("buzz")
            n = n + 1
    
    fizzbuzz(number)