-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
171 lines (121 loc) · 6.28 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Function to perform addition
def addition(num1, num2):
return print(f"\nThe sum of {num1} {operator} {num2} is {round(num1 + num2, 2)}\n")
# Function to perform subtraction
def subtraction(num1, num2):
# Check if num1 is less than num2
if num1 < num2:
# Error handling for choice input
while True:
# Ask user if they want to continue with the subtraction or swap the inputs
choice = input("\nThis will return a minus/negative answer, do you want to continue or swap the inputs around?(y/n): ").lower().strip()
# If user chooses to continue, perform subtraction with original order
if choice == "y":
break
# If user chooses to swap, perform subtraction with swapped order
elif choice == "n":
return print(f"\nThe difference between {num2} {operator} {num1} is {round(num2 - num1, 2)}\n")
# If user provides an invalid input, prompt them to choose between 'y' and 'n'
else:
print("Need to choose between(y/n)")
continue
# If num1 is not less than num2, perform subtraction with original order
return print(f"\nThe difference between {num1} {operator} {num2} is {round(num1 - num2, 2)}\n")
# Function to perform multiplication
def multiplication(num1, num2):
return print(f"The product between {num1} {operator} {num2} is {round(num1 * num2, 2)}\n")
# Function to perform division
def division(num1, num2):
# Check if num2 is 0, which would result in division by zero
if num2 == 0:
# Error handling for choice input
while True:
# Ask user if they want to continue and break the program or swap the inputs
choice = input("\nThis will return infinity, do you want to continue and break the program or swap the inputs around?(y/n): ").lower().strip()
# If user chooses to continue, print a message and perform division with original order
if choice == "y":
print("You are a psychopath!!! Let me ask you again...")
# If the comment(#) below gets removed it will break the code
# return print(f"{round(num1 / num2, 2)}")
continue
# If user chooses to swap, perform division with swapped order
elif choice == "n":
return print(f"\nThe quotient between {num2} {operator} {num1} is {round(num2 / num1, 2)}\n")
# If user provides an invalid input, prompt them to choose between 'y' and 'n'
else:
print("Need to choose between(y/n)")
continue
# If num2 is not 0, perform division with original order
return print(f"\nThe quotient between {num1} {operator} {num2} is {round(num1 / num2, 2)}\n")
# Function to perform modulus
def modulus(num1, num2):
# Check if num2 is 0, which would result in modulus by zero
if num2 == 0:
# Error handling for choice input
while True:
# Ask user if they want to continue and break the program or swap the inputs
choice = input("\nThis will return infinity, do you want to continue and break the program or swap the inputs around?(y/n): ").lower().strip()
# If user chooses to continue, print a message and perform modulus with original order
if choice == "y":
print("You are a psychopath!!! Let me ask you again...")
# If this comment gets removed it will break the code
# return print(f"{round(num1 % num2, 2)}")
continue
# If user chooses to swap, perform modulus with swapped order
elif choice == "n":
return print(f"\nThe remainder between {num2} {operator} {num1} is {round(num2 % num1, 2)}\n")
# If user provides an invalid input, prompt them to choose between 'y' and 'n'
else:
print("Need to choose between(y/n)")
continue
# If num2 is not 0, perform modulus with original order
return print(f"\nThe remainder between {num1} {operator} {num2} is {round(num1 % num2, 2)}\n")
# Function to perform exponentiation
def power(num1, num2):
return print(f"\nThe result between {num1} raised to the power(**) of {num2} is {round(num1 ** num2, 2)}\n")
# Infinite loop to keep asking for user input until a valid operation is performed or valid input is received
# Error handling for num1
while True:
try:
# Get user input for num1
num1 = float(input("\nPlease enter the first number: "))
break
except Exception:
# If the user enters non-numeric input for numbers, catch the ValueError and ask for input again
print("Invalid input! Please enter a numeric value.")
# Infinite loop to keep asking for user input until a valid operation is performed or valid input is received
# Error handling for num2
while True:
try:
# Get user input for num2
num2 = float(input("\nPlease enter the second number: "))
break
except Exception:
# If the user enters non-numeric input for numbers, catch the ValueError and ask for input again
print("Invalid input! Please enter a numeric value.")
# Error handling for operator
while True:
operator = input("\nPlease enter operator (+, -, *, /, %, **): ").strip()
# Perform the corresponding operation based on the operator entered
if operator == "+":
addition(num1, num2)
break
elif operator == "-":
subtraction(num1, num2)
break
elif operator == "*":
multiplication(num1, num2)
break
elif operator == "/":
division(num1, num2)
break
elif operator == "%":
modulus(num1, num2)
break
elif operator == "**":
power(num1, num2)
break
else:
# If an invalid operator is entered, prompt the user to enter a valid one
print("Invalid operator! Please enter a valid operator from these choices (+, -, *, /, %, **)!")
continue