-9

I recently began python and I tried the FizzBuzz test. I came up with this:

count = 0
while count <= 100:
  if (count % 3) == 0:
    print "Fizz"
    count = count + 1
  elif (count % 5) == 0:
    print "Buzz"
    count = count + 1
  elif (count % 5) and (count % 3):
    print "FizzBuzz"
    count = count + 1
  else:
    print count
    count = count + 1

It outputs no numbers :

Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
Buzz
Fizz
FizzBuzz
FizzBuzz
Fizz
Buzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
Buzz
Fizz
FizzBuzz
FizzBuzz
Fizz
Buzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
Buzz
Fizz
FizzBuzz
FizzBuzz
Fizz
Buzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
Buzz
Fizz
FizzBuzz
FizzBuzz
Fizz
Buzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
Buzz
Fizz
FizzBuzz
FizzBuzz
Fizz
Buzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
Buzz
Fizz
FizzBuzz
FizzBuzz
Fizz
Buzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
FizzBuzz
Fizz
FizzBuzz
Buzz
Fizz
FizzBuzz
FizzBuzz
Fizz
Buzz

This is not the correct output. How could I clean up the program?

Derek Wang
  • 10,098
  • 4
  • 18
  • 39
Kai
  • 9
  • 4

2 Answers2

4

Think about the order in which these statements occur,

elif (count % 5) and (count % 3):

this line of code will never execute, as either

if (count % 3) == 0:

or

elif (count % 5) == 0:

will execute first if those conditions are true. In this case you want to check if both of these conditions are true, only then should you check to see if a single condition is true.

Also, the line of code

count = count + 1

appears in every branch of your code, consider placing this somewhere where it will be executed every single time.

However I would choose to use a for loop rather than a while loop,

for x in range(100):

this eliminates the need for an extra count variable.

Another thing to watch out for

elif (count % 5) and (count % 3):

here, you're not checking if the number % 5 is == 0, you're just checking (count % 5). So the expression "if (count % 5)" will result in True if count is not evenly divisible by 5 (check out Truth value testing). The same goes for the other places where you leave out the == comparator

Here's an example of a similar approach to yours.

for count in range(1, 101):
    if count % 5 == 0 and count % 3 == 0:
        print "FizzBuzz"
    elif count % 5 == 0:
        print "Buzz"
    elif count % 3 == 0 and count % 5 == 0:
        print "Fizz"
    else:
        print count

things to note:

  • checking for both conditions before checking for individual conditions
  • for loop instead of while loop (personal preference)
  • checking for == 0
chatton
  • 596
  • 1
  • 3
  • 8
1

I think the logic is to check whether

  1. a number is divisible by both 3 and 5.
  2. a number is divisible by 3 but not 5.
  3. a number is divisible by 5 but not 3.
  4. a number is divisible by neither 5 nor 3.

Declare the Variables. I added extra variables just to be sure that things were stacking up well. You can skip the variables followed by "divisible_by" if you want.

myArray = []
divisible_by_both = []
divisible_by_three = []
divisible_by_five = []
digits = range(1,101)

Write the loop and conditions.

for x in digits:
  if (x % 3 == 0) and (x % 5 == 0):
    myArray.append("Fizzbuzz")
    divisible_by_both.append(x)
  if (x % 3 == 0) and not (x % 5 == 0):
    divisible_by_three.append(x)
    myArray.append("Fizz")
  if not (x % 3 == 0) and (x % 5 == 0):
    divisible_by_five.append(x)
    myArray.append("Buzz")
  if not (x % 3 == 0) and not (x % 5 == 0):
    myArray.append(x)

Print the Result, or print the other array to ensure that the total length is 100.

print(myArray)

Here's the output. You can join the array and output them as string if you like. [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'Fizzbuzz', 16, 17, 'Fizz', 19, 'Buzz', 'Fizz', 22, 23, 'Fizz', 'Buzz', 26, 'Fizz', 28, 29, 'Fizzbuzz', 31, 32, 'Fizz', 34, 'Buzz', 'Fizz', 37, 38, 'Fizz', 'Buzz', 41, 'Fizz', 43, 44, 'Fizzbuzz', 46, 47, 'Fizz', 49, 'Buzz', 'Fizz', 52, 53, 'Fizz', 'Buzz', 56, 'Fizz', 58, 59, 'Fizzbuzz', 61, 62, 'Fizz', 64, 'Buzz', 'Fizz', 67, 68, 'Fizz', 'Buzz', 71, 'Fizz', 73, 74, 'Fizzbuzz', 76, 77, 'Fizz', 79, 'Buzz', 'Fizz', 82, 83, 'Fizz', 'Buzz', 86, 'Fizz', 88, 89, 'Fizzbuzz', 91, 92, 'Fizz', 94, 'Buzz', 'Fizz', 97, 98, 'Fizz', 'Buzz']

deepyes02
  • 48
  • 8