-1

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

Alex Vincent
  • 97
  • 1
  • 3
  • 10
  • See [here](http://stackoverflow.com/questions/14329176/python-loops-extra-print/14329269) and [here](http://stackoverflow.com/questions/7053652/random-none-output-from-basic-python-function). – TigerhawkT3 Mar 01 '17 at 20:29
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Paul Hankin Mar 01 '17 at 20:47

1 Answers1

1
            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)
Kevin
  • 74,910
  • 12
  • 133
  • 166