Beginner Python Question: FIzz Buzz using while loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Beginner Python Question: FIzz Buzz using while loop (/thread-38236.html) |
Beginner Python Question: FIzz Buzz using while loop - camoyn13 - Sep-20-2022 Hello, Having an issue with the Fizz Buzz challenge using the While Loop. Any help would be greatly appreciated. Please note I HAVE TO USE the while loop. Any help would be much appreciated! the 2 issues are: 1. I cannot get past the first conditional ( keeps printing "foobarbaz") 2. I cannot end the loop. MAXVAL = 30 while (i < MAXVAL+1): if i % 2 == 0 and i % 3 == 0 and i % 5 == 0: print ('foobarbaz') elif i % 2 == 0 and i % 3 == 0: print ('foobar') elif i % 3 == 0 and i % 5 == 0: print("barbaz") elif i % 2 == 0 and i % 5 == 0: print('foobaz') # If the number is divisible by 2, print the word bar elif i % 2 == 0: print('foo') # If the number is divisible by 3, print the word bar elif i % 3 == 0: print('bar') #If the number is divisible by 5, print the word baz elif i % 5 == 0: print('baz') #If the number is not divisible by 2,3 or 5, do not print a string elif i % 2 == 1 or i % 3 == 1 or i % 5 == 1: print () RE: Beginner Python Question: FIzz Buzz using while loop - Yoriz - Sep-20-2022
RE: Beginner Python Question: FIzz Buzz using while loop - deanhystad - Sep-20-2022 I think this is what you were trying to do: MAXVAL = 30 i = 1 while (i < MAXVAL+1): if i % 2 == 0 and i % 3 == 0 and i % 5 == 0: print(i, 'foobarbaz') elif i % 2 == 0 and i % 3 == 0: print(i, 'foobar') elif i % 3 == 0 and i % 5 == 0: print(i, "barbaz") elif i % 2 == 0 and i % 5 == 0: print(i, 'foobaz') # If the number is divisible by 2, print the word bar elif i % 2 == 0: print(i, 'foo') # If the number is divisible by 3, print the word bar elif i % 3 == 0: print(i, 'bar') #If the number is divisible by 5, print the word baz elif i % 5 == 0: print(i, 'baz') #If the number is not divisible by 2,3 or 5, do not print a string elif i % 2 == 1 or i % 3 == 1 or i % 5 == 1: print(i) i += 1Your solution should be shorter. My solution to this problem is 3 lines long. There is no reason this program should be more than 7 lines. Try to think of a way to write this program without uses else or elif. If you don't want to do that, at least think about the math. This is horribly inefficient: if i % 2 == 0 and i % 3 == 0 and i % 5 == 0:If a number is divisible by 2, 3 and 5 it is also divisible by 30. If a number is divisible by 3 and 5 it is divisible by 15. If divisible by 2 and 3 it is divisible by 6 This test is not needed: elif i % 2 == 1 or i % 3 == 1 or i % 5 == 1: print(i)If the number was divisible by 2. 3 or 5 it would never reach this point in the program. It would either print foo, bar, baz or some combination. There is no need to test if the number is not divisible by 2, 3 or 5. You should spend more time thinking about the problem you are trying to solve than thinking about the code. Coding is the easy part of programming. |