0

If I set the variable numCount to 100, the program fails to be accurate, yet with a number such as 25, it works perfectly fine. What's going on?

print "Fizzbuzz"
numCount = 0

while numCount < 100:
    numCount = numCount + 1

    if (numCount % 3) == 0:
        if (numCount % 15) == 0:
            print "Fizzbuzz"
        else:
                print "Fizz"
    if (numCount % 5) == 0:
        if (numCount%15) == 0:
            print "Fizzbuzz"
        else:
                print "Buzz"
    else:
        print numCount
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • 5
    Your `while` loop requires the variable to be less than 100, so if you set it to 100, it will never enter the loop. . . – BrenBarn Jul 06 '13 at 02:42
  • 1
    What do you mean by "accurate"? Can you describe what did you expect to happen vs. what actually happened? with details – ilomambo Jul 06 '13 at 02:52
  • My results were, with the loop going through a hundred time, gave me wrong results such as displaying 99, whereas it should have displayed fizz – Jacob Willinger Jul 06 '13 at 03:10

3 Answers3

4

while numCount < 100: means while numCount is less than 100. If numCount == 100 or a higher number, the while loop will not run.

Did you mean <=? <= is the operator for "is less than or equal to".

TerryA
  • 58,805
  • 11
  • 114
  • 143
0

Look at the while loop:

while numCount < 100

You probably already know it means "while numCount is less than 100". That's the trouble. See, if numCount is 25, Python interprets it like this the first time:

while 25 < 100

Now, say numCount is 100. Python interprets that statement like this:

while 100 < 100

See the problem? You're basically saying "while 100 is less than 100". But 100 is never less than 100! Therefore, the loop never runs. You can use <= instead of <. That means "less than or equal too". So, if the contents of numCount or less than or equal to 100, then the loop will run. That's probably what you intended.

kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
0

This question is extremely old, but I wanted to add an answer here, based on something cool I saw here, last week looking up an unrelated topic.

FizzBuzz: For integers up to and including 100, prints FizzBuzz if the integer is divisible by 3 and 5 (15); Fizz if it's divisible by 3 (and not 5); Buzz if it's divisible by 5 (and not 3); and the integer otherwise.

Behold!

def FizzBuzz():
    for i in range(1,101):
        print {
            3 : "Fizz",
            5 : "Buzz",
            15 : "FizzBuzz"}.get(15*(not i%15) or
                                 5*(not i%5 ) or
                                 3*(not i%3 ), '{}'.format(i))

The .get() method works wonders here.

Operates as follows

For all integers from 1 to 100 (101 is NOT included),
print the value of the dictionary key that we call via get according to these rules.

"Get the first non-False item in the get call, or return the integer as a string."

When checking for a True value, thus a value we can lookup, Python evaluates 0 to False. If i mod 15 = 0, that's False, we would go to the next one.

Therefore we NOT each of the 'mods' (aka remainder), so that if the mod == 0, which == False, we get a True statement. We multiply True by the dictionary key which returns the dictionary key (i.e. 3*True == 3)

When the integer it not divisible by 3, 5 or 15, then we fall to the default clause of printing the int '{}'.format(i) just inserts i into that string - as a string.

Some of the output

Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz

Community
  • 1
  • 1
VISQL
  • 1,960
  • 5
  • 29
  • 41