-
Notifications
You must be signed in to change notification settings - Fork 8
/
simple_calculator.py
54 lines (46 loc) · 1.37 KB
/
simple_calculator.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Function to perform addition
def add(x, y):
return x + y
# Function to perform subtraction
def subtract(x, y):
return x - y
# Function to perform multiplication
def multiply(x, y):
return x * y
# Function to perform division
def divide(x, y):
if y == 0:
return "Division by zero is not allowed."
else:
return x / y
# Welcome message
print("Welcome to the Simple Calculator!")
# Input for the first number
try:
num1 = float(input("Please enter the first number: "))
except ValueError:
print("Invalid input. Please enter a valid number.")
exit()
# Input for the second number
try:
num2 = float(input("Please enter the second number: "))
except ValueError:
print("Invalid input. Please enter a valid number.")
exit()
# Input for the operation
operation = input("Select operation (+, -, *, /): ")
# Perform the calculation based on the selected operation
if operation == '+':
result = add(num1, num2)
print(f"Result: {num1} + {num2} = {result}")
elif operation == '-':
result = subtract(num1, num2)
print(f"Result: {num1} - {num2} = {result}")
elif operation == '*':
result = multiply(num1, num2)
print(f"Result: {num1} * {num2} = {result}")
elif operation == '/':
result = divide(num1, num2)
print(f"Result: {num1} / {num2} = {result}")
else:
print("Invalid operation. Please select +, -, *, or /.")