-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
66 lines (57 loc) · 1.63 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
print("Solution 1 (Only works for 2 numbers)")
while True:
try:
num1 = int(input("Enter the first number: "))
except:
print("Please enter a valid number")
continue
break
while True:
try:
num2 = int(input("Enter the second number: "))
except:
print("Please enter a valid number")
continue
break
while True:
operator = input("Enter the operator (+,-,*,/): ")
result = 0
if(operator == "+"):
result = num1 + num2
elif(operator == "-"):
result = num1-num2
elif(operator == "*"):
result = num1*num2
elif(operator == "/"):
result = num1/num2
else:
print("An error occurred, please enter an operator")
continue
print("The result is: " + str(result))
break
print("")
print("Solution 2 (Using python built-in function)")
while True:
try:
operationResult = eval(input("Please enter an operation problem: "))
except:
print("Please enter a valid operation problem")
continue
print("The result is: " + str(operationResult))
break
#Average Calculator
print("")
print("Average Calculator")
def calculateAverage(listOfNumbers):
return sum(listOfNumbers)/len(listOfNumbers)
while True:
try:
inputString = input("Please enter a sequence of number (separated by ','): ")
inputList = inputString.split(",")
inputList = list(map(float, inputList))
except:
print("Please enter a valid sequence (separate the numbers using ','): ")
continue
result = calculateAverage(inputList)
print("The result is: " + str(result))
break