You are viewing our Forum Archives. To view or take place in current topics click here.
FizzBuzz Explained! (Python)
Posted:

FizzBuzz Explained! (Python)Posted:

anthoneyk123
  • Ladder Climber
Status: Offline
Joined: Mar 05, 201014Year Member
Posts: 320
Reputation Power: 12
Status: Offline
Joined: Mar 05, 201014Year Member
Posts: 320
Reputation Power: 12
FizzBuzz is a drinking game where you count up from one, and if the number is divisible by 3 you say Fizz, if it is divisible by 5 you say Buzz, and if it divisible by both 3 and 5 you say FizzBuzz. If you make a mistake, you drink. It is commonly used by interviewers to weed out the applicants with minimal knowledge. Below is the code with comments explaining each line.


print "Welcome to FizzBuzz"
x=input("What number would you like FizzBuzz to go up to?")
#This bit greets the user and asks them what number they want to calculate to
for i in range(1,x):
#This line counts up from 1 to x with x being what is inputted on line 2
        if i%15==0:
                print "FizzBuzz"
#This bit prints FizzBuzz instead of the number if the number is divisible by 15, if it is not, it moves to the next bit
        elif i%5==0:
                print "Buzz"
#This bit prints Buzz instead of the number if it is divisible by 5, if it is not, it moves to the next bit
        elif i%3==0:
                print "Fizz"
#This bit prints Fizz if the number is divisible by 3, if it is not, it moves to the next bit
        else:
                print i
#This bit instructs the computer to just print the number if it does not fall under any of the above classifications
close=raw_input("Press <<ENTER>> to close")
#This line stops command line from closing right away
#2. Posted:
RDCA
  • TTG Contender
Status: Offline
Joined: Jul 12, 201013Year Member
Posts: 3,612
Reputation Power: 173
Status: Offline
Joined: Jul 12, 201013Year Member
Posts: 3,612
Reputation Power: 173
This was posted on XMB, and really there are only two ways people do it mainly. Either like you, four if statements, or like 3 ternary operator statements.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.