Table of Contents

In this Python tutorial, learn to create a intermediate Python calculator for addition, subtraction, multiplication, division and modulus. Within the Python function, one will be able to pass data, which are known as parameters and the function will return the result as data.

## Import Python Modules

The Python modules **sys **(import exit) and **time** will need to be imported to create a few of the Python functions.

1 2 | from sys import exit import time |

## Create Variables, while Loop and Receive User Input Commands

The below will provide variables and a while loop to continoulsy receive commands from the user. The user will be able to complete a series of commands for addition, subtraction, multiplication, division, and modulus. The user will have the ability to input two numbers by picking the first number in the prompt and a second number on the next prompt. Also, the user will only have the choice to enter an integer by using the **int()** function.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | again = "yes" while again == "yes": if again == "no": exit(0) print("What mathematical function would like to use?") print("\n1 - Addition \t 2 - Subtraction \t 3 - Multiplication \t 4 - Division\t 5 - Modulus "); selection = int(input(f"\nEnter your selection: ")) time.sleep(1) print(f"You have selected option: {selection}") time.sleep(1) print("Please pick two numbers: ") num1 = int(input("Pick your first number and press Enter? ")) num2 = int(input("Pick your second number and press Enter? ")) time.sleep(1) print(f"You have chosen number {num1} for your first number") time.sleep(1) print(f"You have chosen number {num2} for your second number") time.sleep(1) |

if…elif Python Statements

The below if…elif statements will execute once the user enters the command for the selection of addition, subtraction, multiplication, division, or modulus.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | if selection == 1: add_numbers = num1 + num2 #Printing addition print(f"The addition of {num1} and {num2} is {add_numbers}") time.sleep(1) elif selection == 2: subtract_numbers = num1 - num2 #Printing subtraction print(f"The subtraction of {num1} and {num2} is {subtract_numbers}") time.sleep(1) elif selection == 3: multiply_numbers = num1 * num2 #Printing multiplication print(f"The multiplication of {num1} and {num2} is {multiply_numbers}") time.sleep(1) elif selection == 4: divide_numbers = num1 / num2 #Printing division print(f"The division of {num1} and {num2} is {divide_numbers}") time.sleep(1) elif selection == 5: modulus_numbers = num1 % num2 #Printing modulus print(f"The modulus of {num1} and {num2} is {modulus_numbers}") time.sleep(1) again = input("Would you like to use the calculator again? ") |

Hope this Python tutorial was helpful in learning how to utilize user command input, while loops and if…elif statements in Python.