-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.py
51 lines (39 loc) · 1.67 KB
/
calc.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
import operator
def operation_select():
operation = input("\nPlease select an operation by typing:\n'+' for addition \n'-' for subtraction \n'*' for "
"multiplication \n'/' for division \n\n")
if operation == "+":
print ("You have selected addition")
elif operation == "-":
print ("You have selected subtraction")
elif operation == "*":
print ("You have selected multiplication")
elif operation == "/":
print ("You have selected division")
else:
while not (operation == "*" or operation == "-" or operation == "+" or operation == "/"):
operation = input("\nPlease select a valid operand! Try again: ")
return operation
def select_digit_one():
i1 = input("\nPlease type the first digit: ")
return i1
def select_digit_two():
i2 = input("\nPlease type the second digit: ")
return i2
def calculation(input1, operand, input2, name):
ops = {"+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv}
if operand == "/" and input2 == "0":
result = float("0")
else:
result = ops[str(operand)](float(input1), float(input2))
print("\nThe result is: ", result)
continuation = input("\nDo you wish to continue? (Y/N): ")
inputs = \
{"yes": ["y", "Y"], "no": ["n", "N"]}
while not (continuation in inputs['yes'] or continuation in inputs['no']):
continuation = input("\nPlease type a valid answer! (Y/N): ")
if continuation in inputs['yes']:
calculation(select_digit_one(), operation_select(), select_digit_two())
elif continuation in inputs['no']:
print("Bye, " + name + " !")
quit()