0% found this document useful (0 votes)
2 views

Calculator

This document outlines a simple Python calculator program that allows users to perform basic arithmetic operations: addition, subtraction, multiplication, and division. Users can input two numbers and select an operation, with error handling for invalid inputs and division by zero. The program continues to run until the user types 'exit'.

Uploaded by

Mohamed Mostafa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Calculator

This document outlines a simple Python calculator program that allows users to perform basic arithmetic operations: addition, subtraction, multiplication, and division. Users can input two numbers and select an operation, with error handling for invalid inputs and division by zero. The program continues to run until the user types 'exit'.

Uploaded by

Mohamed Mostafa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

print("Welcome to the Python Calculator!

")
print("Available operations: +, -, *, /")
print("Type 'exit' to quit the calculator.\n")

exit_program = False # Flag to control the loop

while not exit_program:


# Get the operation from the user
operation = input("Enter an operation (+, -, *, /) or 'exit': ").strip()

# Check if the user wants to exit


if operation.lower() == 'exit':
print("Thank you for using the calculator. Goodbye!")
exit_program = True
continue

# Check for valid operations


if operation not in ['+', '-', '*', '/']:
print("Invalid operation. Please try again.")
continue

# Get the first number


valid_input = False
while not valid_input:
1|Page Calculator Python Grade 7
num1_input = input("Enter the first number: ")
if num1_input.replace('.', '', 1).isdigit() or (num1_input.startswith('-
') and num1_input[1:].replace('.', '', 1).isdigit()):
num1 = float(num1_input)
valid_input = True
else:
print("Invalid input. Please enter a valid number.")

# Get the second number


valid_input = False
while not valid_input:
num2_input = input("Enter the second number: ")
if num2_input.replace('.', '', 1).isdigit() or (num2_input.startswith('-
') and num2_input[1:].replace('.', '', 1).isdigit()):
num2 = float(num2_input)
valid_input = True
else:
print("Invalid input. Please enter a valid number.")

# Perform the calculation


if operation == '+':
result = num1 + num2
elif operation == '-':
result = num1 - num2

2|Page Calculator Python Grade 7


elif operation == '*':
result = num1 * num2
elif operation == '/':
if num2 == 0:
print("Error: Division by zero is not allowed.")
continue
result = num1 / num2

# Display the result


print(f"The result of {num1} {operation} {num2} is: {result}\n")

3|Page Calculator Python Grade 7

You might also like