Free shipping from € 74,95 NL (€ 99,95 BE / DE)

Ordered before 16:00 tomorrow at home

Express ordered before 11:30 am tonight at home*

14 days free return *

Logo Electronics For You


Raspberry Pi project: Programming a calculator in Python 

In this project you will learn how to program a calculator in Python. This is a basic project for the Raspberry Pi. You need the Thonny Python IDE (Integrated Development Environment). This is installed by default on the Raspberry Pi OS. You learn how to apply different statements and functions. With this project you will learn the basic functions of Python. 

This manual covers: 

  • Level - Beginner 30% 30%
  • Duration 30 min 35% 35%
  • Cost - € 70,30 50% 50%

Step 2: Connect & Start Up

Insert the micro SD card (with Raspbian) into the micro SD card slot on the bottom of the Raspberry Pi. There are two Micro HDMI ports on the side of the Raspberry Pi 4B. Insert the Micro HDMI cable into the left port, and the HDMI end into a monitor. Connect a keyboard & mouse to the USB ports and finally insert the power cable into the USB-C port.

Your setup is now ready to start. When you start the Raspberry Pi you will see a desktop. During the first start you have to set a number of data such as language, time, internet, etc. This is explained step by step and you only have to do it once.

You start the Python IDE by selecting the “Thonny Python IDE” at the main menu, under “Programming”. You will then see this screen (figure 1):

Here you can see the Raspberry Pi imager. Click on the left button. It is labeled "CHOOSE OS".

Figure 1

Step 3: Functions and statements

Python is the common programming language for software writing in Raspberry Pi. This example code is therefore written in Python. In the sample code we use various functions and statements. Many of these functions may be unknown, so we will cover these functions one by one. Note that Python is case sensitive. This means that Python distinguishes between the input of words with and without capital letters. For example, some functions don't work if you type the word with a capital letter, because it should be with a lowercase letter or vice versa.

 

Functions and statements in the sample code: 

Step 4: programming

You can copy and paste the code below. We recommend that you copy the code yourself as you will learn more from it. Don't forget to put the code in a logical place. Once you have entered and saved the sample code (see figure 2), you can start the code. You do this by pressing ctrl + t or by selecting “Run current script in terminal” under the heading “Run”. When you start the code, you will first reach the menu choice. You have to make a choice which function you want to use. Then enter your desired number and press enter. Then two numbers are asked that you want to use for the sum. The answer of the sum is printed one line below (see figure 3). If the code gives an error, you can look it up by using ctrl + F5, or by selecting “Debug current script (nicer)” under “Run”.

menu choice = 0 #Variable menu choice must be a number between 0 and 7
while menu selection in (0,1,2,3,4,5,6,7):
    print (“Menu choice:”)
    print (“1: Exponentiation 2: Multiplication 3: Divide 4: Whole division”)
    print (“5: Calculate the remainder of the division 6: Addition 7: Subtract”) #The menu options are printed
    menu choice = int (input (“Make a choice:“))
    print (“”)
    if menu choice> 7 or menu choice <1:
        print (“wrong menu choice”)
        print (“”)
        menu choice = 0 # back to menu choice
    else:
        a = float (input (“give number a:“))
        b = float (input (“give number b:“))
    if menu choice == 1:
        c = a ** b #Enhance power
        print (a, "^", b, "=", c)
        print (“”)
    elif menu choice == 2:
        c = a * b #Multiply
        print (a, "*", b, "=", c)
        print (“”)
    elif menu choice == 3:
        c = a / b #Share
        print (a, "/", b, "=", c)
        print (“”)
    elif menu choice == 4:
        c = a // b # Whole division
        c_rounded = format (c, '. 0f') #Variable c, rounded to 0 decimal places
        print (“Whole number”, a, ”/”, b, ”=”, c_rounded)
        print (“”)
    elif menu choice == 5:
        c = a% b #Mudulo
        c_rounded = format (c, '. 0f') #Variable c, rounded to 0 decimal places
        print (“Remaining number”, a, ”/”, b, ”=”, c_rounded)
        print (“”)
    elif menu choice == 6:
        c = a + b # Addition
        print (a, "+", b, "=", c)
        print (“”)
    elif menu choice == 7:
        c = ab #Subtract
        print (a, ”-“, b, ”=”, c)
        print (“”)

Figure 2

Figure 3

If you have successfully completed this project, you will know how to use various functions in Python. So which calculations you can make, and which arithmetic operators you need. You can use this later on many other projects.

footnotes

The functions and variables used in the sample code are explained here.

remarks:
Sometimes it is useful to put a comment / note in the code. You can do this by means of a #. The text that you type after the # has no further influence on the code. If you want to have several lines in a row as a comment, you can do this by writing the comment between double quotation marks (“” ”)

Stories:
# This is a comment

"" "
This is a comment,
consisting of several lines.
"" "

variables:
Variables are letters or words, in which you can store data values. In such a variable you can store a number, a word or another variable. If you want to put a word in a variable, you have to put it in quotation marks (”“). A variable is created when you first assign a value to it. A variable can be useful if it has a changeable word or number associated with it.

Stories:
a = 5
b = "Electronics for you".

print ():
The print () is a function that shows the variable or the text that you have placed between the brackets on the screen. For this you must first start the code.

input ():
The input () function does the same as the print () function, but an input is immediately requested. If you want a number to be entered, put an int, float or complex before the 'input'. This depends on what kind of number you want. As a user you have to enter a word or number as an answer, by typing it in and then clicking enter.

int:
You only use an int (integer) for whole, positive and negative numbers.

float:
You use a float for comma numbers.

complex:
You use a complex for complex numbers.

round off floats:
If you want a float to be rounded, you can do this by means of the function; format (variable, '. 1f'). In this case, round to 1 decimal place. If you want to round to 2 decimal places, write format (variable, '. 2f') etc.

f:
Literally means 'if'. This is a Python way of saying, 'if the condition (which is after the' if ') is true, then run the code below it. If the condition is not true, Python will skip this code.

elif:
(else if) Literally means 'other if'. This is a Pythons way of saying, 'if the previous condition is not true, see if this condition is true. If this condition is indeed true, Python will run the code listed under the elif. If this condition is false, Python will skip this code.

else:
Literally means 'different'. This is a Python way of saying, 'if the previous condition is not true, then you should run this code'.

relational operators:
You use a relational operator to compare two values. These equations often appear in if equations. See the table below for the relational operators.

Relational operators:
Is greater than
Is less than
>= Is greater than or equal to
<= Is less than or equal to
== Is equal to
!= Is not equal to

arithmetic operators:
You use an arithmetic operator to make sums. Examples of these operators see the table below.

Arithmetic Operators:
+ Plus
- Min
* Keer
/ Giving away from what you received
% Modules (remainder of division sum)
// Returns an integer after division rounded down
** exponent

while:
A loop is a piece of code that is repeated over and over. With a while loop you can repeatedly execute a piece of code. The while does this as long as the condition after the 'while' is true. If the condition is not (anymore) true, the loop ends.

Sign up for exclusive offers!
SIGN UP

Are you already a member of our newsletter?

Stay tuned for:
  • New products
  • Deals
  • Events
  • News
  • Projects
close-link
The rating of www.elektronicavoorjou.nl at webwinkelkeurmerk.com is 9.2/10 based on 811 reviews.