home

Scientific Computing (Psychology 9040a)

Fall, 2020

A Solution: Fizz Buzz

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

The key is to test for divisibility by 3 and by 5, first. If you test for divisibility by 3 or for divisibility by 5, before, then those cases will be erroneously triggered for the “both” case as well. Try it.

Also: range(101) is needed to get all the way to 100. The 101 here means 101 integers, starting at 0.