Python Forum
Beginner Python Question: FIzz Buzz using while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner Python Question: FIzz Buzz using while loop
#1
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 ()
Reply
#2
  • In the code shown i is never defined
  • i is never changed inside the while loop so it will continue doing the same thing
Reply
#3
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 += 1
Your 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Many iterations for loop question adesimone 9 1,856 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,616 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  Repeat question (for loop) rs74 7 6,507 Jun-17-2020, 03:17 PM
Last Post: rs74
  Question about running comparisons through loop from input value Sunioj 2 2,414 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  *Ultra beginner math question Markg2 2 2,252 Apr-21-2019, 11:24 PM
Last Post: Markg2
  Beginner Python Homework Question (Calculate Gross Pay) matchamochi7 4 5,686 Nov-02-2018, 01:06 PM
Last Post: buran
  Beginner's String Operation question docboy 6 3,492 Sep-30-2018, 05:49 PM
Last Post: j.crater
  while loop question Tripler 4 2,962 Jul-24-2018, 06:37 AM
Last Post: buran
  Loop question kraven 3 3,642 Sep-10-2017, 07:31 AM
Last Post: wavic
  Question about loop Pires 4 3,584 Jul-23-2017, 03:01 AM
Last Post: Pires

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020