-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment3
More file actions
67 lines (51 loc) · 1.94 KB
/
assignment3
File metadata and controls
67 lines (51 loc) · 1.94 KB
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
class SimpleCalculator:
def __init__(self, name):
self.name = name
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def multiply(self, a, b):
return a * b
def divide(self, a, b):
if b == 0:
return 'Syntax Error! Division by zero is prohibited'
else:
return a / b
def greet_user(self):
print("Hello, " + self.name + "'!' Welcome to my Simple Calculator.")
def run_calculator(self):
while True:
print("Choose an operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Exit")
choice = input("Select a choice (1, 2, 3, 4, 5): ")
if choice in ('1', '2', '3', '4'):
num1 = float(input("please Input the first number: "))
num2 = float(input("please Input the second number: "))
if choice == '1':
print(num1, "+", num2, "=", self.add(num1, num2))
print("continue calculating?")
elif choice == '2':
print(num1, "-", num2, "=", self.subtract(num1, num2))
print("continue calculating?")
elif choice == '3':
print(num1, "*", num2, "=", self.multiply(num1, num2))
print("continue calculating?")
elif choice == '4':
print(num1, "/", num2, "=", self.divide(num1, num2))
print("continue calculating?")
elif choice == '5':
print("Closing Calculator.... ")
print("Goodbye " + self.name)
break
else:
print("Invalid Input")
# Main program
name = input("Enter your name, please: ")
calculator = SimpleCalculator(name)
calculator.greet_user()
calculator.run_calculator()