-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
86 lines (68 loc) · 3.35 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
#this function will do the calculations
def calculator() :
while True :
#checks to see if there are any value errors
try :
x = int(input('Please enter first number : ')).strip()
#the while loop validates if the operator is correct
while True :
operator = input('please choose a operator (*, /, -, +) : ').strip()
if operator not in ['*','/','-','+'] :
print('please enter a valid operator')
continue #restarts the loop if a correct operator is not entered
break #stops the while loop if the operator is correct
y = int(input('Please enter second number : ')).strip()
calc = 0
#does the calculations based of the operator
if operator == '/' :
calc = calc + (x / y)
elif operator == '+' :
calc = calc + (x + y)
elif operator == '-' :
calc = calc + (x - y)
elif operator == '*' :
calc = calc + (x * y)
#this while loop validates if the user picks yes or no
#if not then privides an error message
while True :
con = input('do you wanna continue (y/n)? ').strip().lower()
if con not in ['y','n'] :
print('please pick the correct input')
continue
break
#the if statement chekc if the user wants to continue or not
#if they don't then if returns the calculation
if con == 'n' :
return calc
# the while loop will allow the user to continue calculating
# as long as they say yes
while con != 'n' :
#the while loop validates if the operator is correct
while True :
operator = input('please choose a operator (*, /, -, +) : ').strip()
if operator not in ['*','/','-','+'] :
print('please enter a valid operator')
continue #restarts the loop if a correct operator is not entered
break #stops the while loop if the operator is correct
num = input('please enter a number : ').strip()
#does the calculations based of the operator
if operator == '/' :
calc = calc + num
elif operator == '+' :
calc = calc + num
elif operator == '-' :
calc = calc + num
elif operator == '*' :
calc = calc + num
#this while loop validates if the user picks yes or no
#if not then privides an error message
while True :
con = input('do you wanna continue (y/n)? ').strip().lower()
if con not in ['y','n'] :
print('please pick the correct input')
continue #restarts the the loop if correct input is not entered
break # stops the loop id the correct input is entered
return calc
except ValueError :
print('please enter the correct input')
print(calculator())