In this source code example, we will write a simple calculator program in Python that performs basic arithmetic operations such as addition, subtraction, multiplication, and division.
Python Simple Calculator Program
Refer to the comments for code explanation:
def calculator():
# The while loop is used to keep the calculator running until the user chooses to quit
while True:
# Display options for the user
print("\nOptions:")
print("Enter 'add' to add two numbers")
print("Enter 'subtract' to subtract two numbers")
print("Enter 'multiply' to multiply two numbers")
print("Enter 'divide' to divide two numbers")
print("Enter 'quit' to end the program\n")
# Get user input to determine the operation
user_input = input(": ")
# If the user inputs "quit", the while loop is broken and the calculator ends
if user_input == "quit":
break
# Perform addition
elif user_input == "add":
# Get the first number from the user
num1 = float(input("Enter a number: "))
# Get the second number from the user
num2 = float(input("Enter another number: "))
# Add the two numbers and store the result
result = num1 + num2
# Display the result
print("The answer is:", result)
# Perform subtraction
elif user_input == "subtract":
# Get the first number from the user
num1 = float(input("Enter a number: "))
# Get the second number from the user
num2 = float(input("Enter another number: "))
# Subtract the two numbers and store the result
result = num1 - num2
# Display the result
print("The answer is:", result)
# Perform multiplication
elif user_input == "multiply":
# Get the first number from the user
num1 = float(input("Enter a number: "))
# Get the second number from the user
num2 = float(input("Enter another number: "))
# Multiply the two numbers and store the result
result = num1 * num2
# Display the result
print("The answer is:", result)
# Perform division
elif user_input == "divide":
# Get the first number from the user
num1 = float(input("Enter a number: "))
# Get the second number from the user
num2 = float(input("Enter another number: "))
# Divide the two numbers and store the result
result = num1 / num2
# Display the result
print("The answer is:", result)
# If the user inputs an unknown operation, display an error message
else:
print("Unknown input")
# Call the calculator function to start the program
calculator()
Output:
Options:
Enter 'add' to add two numbers
Enter 'subtract' to subtract two numbers
Enter 'multiply' to multiply two numbers
Enter 'divide' to divide two numbers
Enter 'quit' to end the program
: add
Enter a number: 10
Enter another number: 20
The answer is: 30.0
Options:
Enter 'add' to add two numbers
Enter 'subtract' to subtract two numbers
Enter 'multiply' to multiply two numbers
Enter 'divide' to divide two numbers
Enter 'quit' to end the program
: subs tract
Enter a number: 20
Enter another number: 19
The answer is: 1.0
Options:
Enter 'add' to add two numbers
Enter 'subtract' to subtract two numbers
Enter 'multiply' to multiply two numbers
Enter 'divide' to divide two numbers
Enter 'quit' to end the program
: multiply
Enter a number: 2
Enter another number: 3
The answer is: 6.0
Options:
Enter 'add' to add two numbers
Enter 'subtract' to subtract two numbers
Enter 'multiply' to multiply two numbers
Enter 'divide' to divide two numbers
Enter 'quit' to end the program
: divide
Enter a number: 3
Enter another number: 2
The answer is: 1.5
Options:
Enter 'add' to add two numbers
Enter 'subtract' to subtract two numbers
Enter 'multiply' to multiply two numbers
Enter 'divide' to divide two numbers
Enter 'quit' to end the program
: quit
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course