-
Notifications
You must be signed in to change notification settings - Fork 9
/
calculator.py
48 lines (40 loc) · 1.29 KB
/
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
# Custom function to read the two numbers.
def read_numbers():
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
return num1, num2
# Simple menu.
print('Welcome to the calculator!')
print('Please choose an operation:')
print(' 1. Addition')
print(' 2. Subtraction')
print(' 3. Multiplication')
print(' 4. Division')
print(' 5. Exit')
# Main loop.
while True:
choice = input('\nEnter your choice (1-5): ')
if choice in ('1', '2', '3', '4', '5'):
if choice == '1':
num1, num2 = read_numbers()
result = num1 + num2
print(f'Result: {result}')
elif choice == '2':
num1, num2 = read_numbers()
result = num1 - num2
print(f'Result: {result}')
elif choice == '3':
num1, num2 = read_numbers()
result = num1 * num2
print(f'Result: {result}')
elif choice == '4':
try:
num1, num2 = read_numbers()
result = num1 / num2
print(f'Result: {result}')
except ZeroDivisionError:
print("Error: division by zero")
elif choice == '5':
break
else:
print('Invalid Input')