MIPS | Malaysian Informatics And Programming Society


We start with the Fizz Buzz programming challenge. Your task is to write a program that prints numbers from S to E (inclusive of both S and E). But for multiples of 3, print Fizz instead of that number and for multiples of 5, print Buzz. For numbers which are both multiples of 3 and 5, print FizzBuzz.

Input Format:


Output Format:


Where each line contains either a number, Fizz, Buzz or FizzBuzz, as per the task statement.

Sample Input:


Sample Output:



The first thing to do when attempting to solve any computing contest problem is to think of a solution.For this problem, all we have to do is to simulate the process. The problem statement already tells us the behavior we need to program - what remains is for us to translate it into code.## Printing and Loops

First, let us print all the numbers from S=8 to E=16 (inclusive). For now, we will not worry about how to print Fizz, Buzz or FizzBuzz.So how do we even print a number? To print anything in Python we use the print() function. The print() function prints whatever is enclosed between the parenthesis. We use the newer version of Python, namely Python 3. In Python 2, print can be called without the parenthesis.

We have embedded some Python 3 code using an online interpreter called repl.it. Try clicking the “run” button to see what the code prints. After that, you can click “edit on repl.it” to play around with the code. We highly encourage you to do so!

You can print strings like Hello World by putting them in between quotes.

Note: You may have noticed that when running the above code, the last line of output is None. You can safely ignore this. (If you’d like to know, it is the result of the repl.it interpreter trying to print out the return value of the program, which in this case is None.) You can also print numbers.

Now, we have a way to print all the numbers from 8 to 16.

What’s wrong with this? Firstly, it is incredibly tedious. But more importantly this method doesn’t work if we’re trying to print a range that is variable, with different values of S and E. (A common mistake beginners make is forgetting that they are not trying to solve a specific instance of a problem, but the general problem.) A better way to print all the numbers from 8 to 16 would be to use for loops.

The range() function, when given two numbers as input, results in a range that begins with the first number and ends right before the second number. For example, range(8,17) corresponds to the numbers from 8 to 16 (inclusive) because 16 is right before 17. Hence, the code above is basically saying “for each number in this range, print it out”.Now, try printing all numbers given S and E values. Go ahead, click edit and try it out yourself.

Note: The # sign starts a comment line. This is only meant for humans to read, so that we can understand the code better. The computer interpreter will ignore it. For more info, see Python Basic Syntax.

Our Solution: https://repl.it/BGgN/1. Note that this is our code, which is one of many valid codes. Your code might be valid too even if it is written differently. For instance, here is another equally valid way to write the code. Check what your code prints v.s. what our code prints, and make sure it is the same. Understand the differences in coding style, if any.


Now, let us try to make our program print Fizz, Buzz or FizzBuzz when appropriate. To do so, we can use if/else statements. Here is some simple code checking if n is greater than 10. If it is, then the block of code which is part of the indented if-block will execute.

We can also get the code to do something in the case that n is not greater than 10.

We can also add additional branching by using the elif statement.

To check if a number is divisible by some other number, we can use the % operator. a % b returns the remainder when a is divided by b.

Now you know enough to deal with the Fizz and Buzz. But what about the FizzBuzz case? To check that a number is both divisible by 3 and 5, we can use the and operator.

Now, try printing all numbers given S and E values. Replace all numbers divisible by 3 with Fizz, all numbers divisible by 5 with Buzz, and all numbers divisible by both 3 and 5 with FizzBuzz. Once again, click edit and try it out yourself.

Here is iur solution: https://repl.it/BGgN/3


All we are missing now is some way to get input from the keyboad for S and E. To get input, use the input() function. The input() reads input from the keyboard, and returns the value of the input. To store the value, just assign it to a variable, eg. x = input(). To be able to type in input, please open the following code on the repl.it site by clicking “edit on repl.it” before running it.

However, input() reads keyboard input as a string by default. For our purposes, we’d like to read integers. To do so, we can call the int() method, which will typecast the input as an integer. The type() function can tell us what the type of a variable is.

Now, you can solve the FizzBuzz task in its entirety.

Our Solution: https://repl.it/BGgN/5

Now, you should have enough programming knowledge to solve Divisibilities on our practice site. Go ahead and try to solve it!