-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW-Reduce-Example.py
More file actions
64 lines (50 loc) · 1.98 KB
/
Copy pathW-Reduce-Example.py
File metadata and controls
64 lines (50 loc) · 1.98 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
57
58
59
60
61
62
63
64
# No touchy. Can't recreate reduce function. T_T
from
from time import sleep
def get_int(prompt):
while True:
try:
n1 = input(prompt)
if n1 == "":
return "finish"
if n1 == "finish":
return "finish"
return int(n1)
except ValueError:
print("Please enter a valid number!")
def get_nums():
while True:
num = get_int(f"\nEnter your values one at a time.\nPress enter without a value to finish.\n>>>>> ")
if num == "finish":
break
yield num
operations = {
'+': lambda x, y: x + y, # Lambda is simply a placeholder for the function name.
'-': lambda x, y: x - y, # Since this is a dictionary, we can call these functions by their keys.
'*': lambda x, y: x * y, # The Keys are the beginning section of each line
'/': lambda x, y: x / y, # <function key>: <function name and arguments go here>: <function goes here>
'%': lambda x, y: x % y # lambda creates anonymous functions. (Functions not bound to a name.)
}
# MORE LAMBDA EXPLANATION
# def f(x): return x**2
# For example we could quickly create a variable based function by typing: <name> = lambda x: x**2
while True:
print("This is a calculator")
while True:
oper = input(
f'Enter the operation symbol you wish to perform,\nOperations: [{", ".join(operations.keys())}]\n>>> ')
if oper in operations:
break
print('\nPick a proper operator! Please wait...\n')
sleep(2)
print('Enter all the values you wish to process:\n')
while True:
try:
result = process_ints(operations[oper], get_nums()) # Line 42
break
except TypeError:
print('At least enter one value to compute a result!')
print(f'The result of applying {oper} to the values is:\n{result}\n')
again = input('Type "exit" to terminate, or press enter to start again.')
if again == "exit":
break