-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_calculator.py
86 lines (71 loc) · 2.56 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
This program allows the user to select an operation (addition, subtraction, multiplication
or division) and two numbers. The program then performs the selected operation on the two
numbers and displays the result.
Copyright: Adam Nix (2024)
"""
def menu_system():
"""
Displays the menu system and returns the user's choice.
"""
print("welcome to the simple calculator")
print("1. addition")
print("2. subtraction")
print("3. multiplication")
print("4. division")
print("5. exit")
while True:
choice = int(input("Please enter your choice: "))
if choice == 1:
print("You have selected addition")
return addition()
elif choice == 2:
print("You have selected subtraction")
return subtraction(choice)
elif choice == 3:
print("You have selected multiplication")
return multiplication(choice)
elif choice == 4:
print("You have selected division")
return division(choice)
else:
print("Invalid choice (please select 1, 2, 3, or 4)")
def addition():
"""
Performs addition on two numbers.
"""
num1 = int(input("Please enter the first number: "))
num2 = int(input("Please enter the second number: "))
total = num1 + num2
print("The result of adding", num1, "and", num2, "is", total)
def subtraction(choice, num1=None, num2=None):
"""
Performs subtraction on two numbers.
"""
if choice == 2:
num1 = int(input("Please enter the first number: "))
num2 = int(input("Please enter the second number: "))
total = num1 - num2
print("The result of subtracting", num1, "and", num2, "is", total)
def multiplication(choice, num1=None, num2=None):
"""
Performs multiplication on two numbers. (Add logic here)
"""
if choice == 3:
num1 = int(input("Please enter the first number: "))
num2 = int(input("Please enter the second number: "))
total = num1 * num2
print("The result of multiplying", num1, "and", num2, "is", total)
def division(choice, num1=None, num2=None):
"""
Performs division on two numbers.
"""
if choice == 4:
num1 = int(input("Please enter the first number: "))
num2 = int(input("Please enter the second number: "))
while num2 == 0:
print("Error: Cannot divide by zero.")
num2 = int(input("Please enter a non-zero second number: "))
total = num1 / num2
print("The result of dividing", num1, "and", num2, "is", total)
menu_system()