-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-calculator.py
More file actions
34 lines (28 loc) · 1007 Bytes
/
basic-calculator.py
File metadata and controls
34 lines (28 loc) · 1007 Bytes
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
28
29
30
31
32
33
34
# Simple Calculator Program for Beginners
# Enter the first number
num1 = input("Enter the first number: ")
# Enter the second number
num2 = input("Enter the second number: ")
# Choose a mathematical operation: +, -, *, or /
operation = input("Enter the operation (+, -, *, /): ")
# Convert the string inputs to float numbers to perform calculations
num1 = float(num1)
num2 = float(num2)
# Perform the calculation based on the chosen operation
if operation == "+":
result = num1 + num2
elif operation == "-":
result = num1 - num2
elif operation == "*":
result = num1 * num2
elif operation == "/":
# Check for division by zero
if num2 == 0:
print("Error: Cannot divide by zero!")
exit() # Exit the program if division by zero
result = num1 / num2
else:
print("Invalid operation!")
exit() # Exit the program if the operation is not recognized
# Print the result in the format: num1 operation num2 = result
print(f"{num1} {operation} {num2} = {result}")