Simple Remainder Calculator in Python 3

by RedKing9132 in Circuits > Software

6497 Views, 36 Favorites, 0 Comments

Simple Remainder Calculator in Python 3

temp_-2028266494.jpg
Hello everyone,

I am back with another Instructable, this time about coding (or programming). I have been learning an awesome new coding program, Python 3. It is a really popular language and is used by Microsoft, Google, Disney and NASA (apparently). I have only been using books and the Internet to teach myself. I have been experimenting with Python and it has been really fun.
In this Instructable I will be teaching you how to make a simple program in Python that will calculate the remainder and answer when you divide two numbers. Okay, that sounded really dodgy... what I'm saying is that you enter two numbers (like 4 divided by 3) and it would come up with something like '1 remainder 1'.
Let's get straight into this Intructable now.

Installing Python

temp_-1358929711.jpg
temp_-404503757.jpg
temp_-828944646.jpg
Python is completely free for Mac, Windows and Android.
To install Python, go to this website: https://www.python.org/downloads/ and download the version of Python that starts with a 3, because that is the version I will be using here.
I am using an Android tablet. For Android, jump on the Google Play store and search 'QPython3'. It should be the first result.
I am sorry iOS users, you will have to buy Python for iOS.
Please look at the pictures to make sure you have the right version.

Create a New File

temp_1019628624.jpg
I'm sorry, I can't explain this very well. What you need to do is make a new file (also called a script) and name it something like 'Remainder Calculator.py'. You can do this easily on Android by using a file manager app like ES File Explorer or ASTRO File Manager. On QPython3 you can alternatively press on 'Editor' and then press the icon that looks like a page with a '+' in the middle. NB: You can name the file whatever you like, but it always has to end with the extension .py.

Open the File

temp_-1684939215.jpg
temp_64767926.jpg
Open the file so you are able to edit it. I would recommend you use a 3rd-party app to edit it, because it will probably colour code your code and automatically insert indents (we'll talk about these soon). If you don't, open the file as a text file. If I haven't explained what you specifically need to do, I'm sorry, you will have to work it out yourself. If you are using QPython3, look for the file under 'Programs' and open it.

Let's Start Coding

temp_1451531219.jpg
First, type in:
def remaindercalculator():

This is a function. It tells Python to do things inside it when the function is called.
Press enter when you have finished typing.

What Does the Function Do?

temp_1197614133.jpg
Now for some trickier code:
number1 = int(input("Enter the first number: "))
number2 = int(input(number1 + " divided by: "))

Make sure you have exactly 4 spaces before each of these lines. This tells Python that they are inside the function. Note the other spaces in the text? Make sure you include them.
Press enter when you have finished typing.

Calculating the Answer

temp_1988003830.jpg
Type this in (remember to put 4 spaces at the start of each line if you need):
answer1 = number1 // number2
answer2 = number1 % number2

The first line divides the two numbers.
The second line calculates the remainder of the two numbers.
Press enter when you have finished typing.

Displaying the Answer

temp_-1192478759.jpg
Nearly there! Type this in:
print(str(answer1) + " remainder " + str(answer2))

This code will tell Python to display the answer when you use it. e.g. '56 remainder 8'
Press enter when you have finished typing.

Handling Errors

temp_557783682.jpg
This step is optional. This will probably prevent errors happening if the user types in a letter instead of a number.

try:
remaindercalculator()
except:
print("There was an error.")
remaindercalculator()

The second, fourth and fifth line have 4 spaces at the start. The first and third line have none.

}{}{ If You Skipped the Last Step }{}{

temp_-1086562446.jpg
If you skipped the last step, do this step. It's as easy as 1, 2... whatever comes next. Type:
remaindercalculator()

There are no spaces before this line.

Conclusion

temp_1772617045.jpg
temp_-1482157575.jpg
You are finished! Now all you have to do is save the file and run it on Python!
When it asks you, type in a number and press enter. If you type in a symbol or letter (and did not use the error handling step), the output will be 'There was an error.'
This Instructable took a while to construct, so please leave a favourite, comment and maybe even follow me...! If your code does not work (i.e. has bugs in it :] ), please leave the error message in the comments or email me at redking9132@gmail.com and I will try my best to fix it!
More Python tutorials coming out soon...

RedKing9132 out!