# Creating a calculator in Python

Python is a programming language that is a great tool for working with numbers and mathematical algorithms. These features can be applied when creating simple effective programs, for example, a calculator. In the article, we will consider one of the options for the formation of python codes, so that the result is a calculator program.

## Writing calculator code

First, you need to ensure that Python 3 is installed on the local computer. It must also have a programming environment.

The first step is to prompt users for input. It is necessary to write a program when the user enters the numbers with which he will work. + input () + is a built-in function that is used for this purpose, which performs the perception of typing from the keyboard. In this case, the user enters two numbers to query in the program. When you enter, you must insert a space at the end of the line. This is required to include a space between the tooltip line and user input.

number_1 = input(‘Enter your first number: ‘)

number_2 = input(‘Enter your second number: ‘)

Two lines are written, you need to save the data before the next application. The + calculator.py + command allows you to launch the calculator for display in a terminal window. In the programming environment, we use the query + python calculator.py +. When the system prompts you to enter a response, you must enter it in the appropriate terminal window.

OutputEnter your first number:

Enter your second number:

**When you type non-compliant characters, an error code occurs:**

OutputEnter your first number: sammy

Traceback (most recent call last):

File «testing.py», line 1, in <module>

number_1 = int('Enter your first number: '))

ValueError: invalid literal for int() with base 10: 'sammy'

At the moment, there are two integers, but you can also experiment with non-integer digits using the function + int () +

The second step will be the introduced mathematical function in the form of operators. Four mathematical operations need to be added: addition, subtraction, multiplication, division. To properly format the text and provide feedback, we use string formatters.

number_1 = int(input(‘Enter your first number: ‘))

number_2 = int(input(‘Enter your second number: ‘))print(number_1 + number_2)

At this point, in addition to addition to addition, you can add other operators.

number_1 = int(input(‘Enter your first number: ‘))

number_2 = int(input(‘Enter your second number: ‘))# Addition

print(‘{} + {} = ‘.format(number_1, number_2))

print(number_1 + number_2)# Subtraction

# Multiplication

# Division

The third step is conditional operators. In the program, the user must be able to choose the operator between all the options. Therefore, you need to add data at the top of the program. The string is divided into several different ones using triple quotation marks:

Please type in the math operation you would like to complete:

+ for addition

— for subtraction

* for multiplication

/ for division

Given that we ask a person to enter data, it is necessary to use + input () +. Put this function in a string and pass it to a variable called + operation +.

number_1 = int(input(‘Enter your first number: ‘))

number_2 = int(input(‘Enter your second number: ‘))print(‘{} + {} = ‘.format(number_1, number_2))

print(number_1 + number_2)print(‘{} — {} = ‘.format(number_1, number_2))

print(number_1 — number_2)print(‘{} * {} = ‘.format(number_1, number_2))

print(number_1 * number_2)print(‘{} / {} = ‘.format(number_1, number_2))

print(number_1 / number_2)

Now it does not matter what a person enters, so you need to add conditional statements to the program. The + if + statement will be placed at the junction point, else-if or + elif + for other operators and + else + will be set to handle errors.

Due to structuring, when the "+% +" sign is introduced into the first prompt, the user will receive feedback to retry only when entering numbers. When you enter numbers into a query, the program performs an operation and shows the result.

At this point, the program is ready and functional, but you can perform the second and subsequent operations only when you run it again. The manual describes a primitive way to create a calculator program in python. The code can be supplemented and improved so that the program allows you to make calculations as many times as possible.

There are many options to improve the functionality of the calculator program, there are various ways to fix errors. It is worth noting that there is no one correct algorithm for solving problems and writing programs. Each user has the right to add their own code improvements and individually approach the creation of a calculator in the Python program.