Search code examples
pythonif-statementconditional-statementsrepeatfizzbuzz

While loop ignores conditionals (if, else) and just prints first suggested print option


I am trying to create a program that prints out a list of numbers starting at 0 and leading up to a number the user inputs (represented by the variable "number"). I am required to use "while" loops to solve this problem (I already have a functional "for" loop version of the assignment). The program should mark anything in that list divisible by 3 with the word "Fizz," divisible by 5 with the word "Buzz," and anything divisible by both with "FizzBuzz" while also including unlabeled numbers outside of those specifications.

Every time I run this program, it ignores the conditions and just prints the word "FizzBuzz" however many times is represented by the number inputted. (I typically use 15 because it has at least one example of each condition, so that means I get 15 "FizzBuzz"s in a row).

To find out why it was doing that, I used print(i) instead of the rest of the program under the first conditional and it gave me 15 counts of the number 0, so there is reason to believe the program is completely ignoring the range I gave it and just outputting copies of i based on the user number input.

Any help would be appreciated!

number = int(input("Enter a Number"))
i = 0
while(i < number + 1):
    if number % 3 == 0 and number % 5 == 0:
        print("Fizzbuzz")
    elif number % 5 == 0:
        print("Buzz")
    elif number % 3 == 0:
        print("Fizz")
    else:
        print(number)
    i += 1
print ("Done!")

Solution

  • You meant to check the divisibility of i, which increments every loop, not of number which doesn't change.

    You also meant to print(i) in the else clause.