This repository was archived by the owner on Apr 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
56 lines (44 loc) · 1.4 KB
/
Copy pathinterface.py
File metadata and controls
56 lines (44 loc) · 1.4 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
def print_operations(operations):
print()
for i, operation in enumerate(operations):
print("%i - %s" % (i+1, operation))
print("C - Clear\nQ - Quit")
def clean_calculator(operations):
print("\nCleared.\n\n")
ans = float(input('\nInsert a number: '))
return menu(operations, ans)
def quit_calculator():
print("Good bye!")
return None
def get_operation_by_index(operations, index):
option = list(operations.keys())[index - 1]
print('\n' + option.upper())
return option
def get_result(function, a):
try:
return function(a)
except:
number = float(input('\nInsert a number: '))
try:
return function(number)
except:
return function(a, number)
def print_result(result, operations):
if result:
if result.is_integer():
print('\n RESULT:', int(result))
else:
print('\n RESULT:', result)
else:
return clean_calculator(operations)
def menu(operations, ans=None):
print_operations(operations)
option = input('\nChoose an operation: ')
if option.upper() == "C":
return clean_calculator(operations)
if option.upper() == "Q":
return quit_calculator()
chosen = operations[get_operation_by_index(operations, int(option))]
result = get_result(chosen, ans)
print_result(result, operations)
menu(operations, result)