-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserman2Example.py
More file actions
35 lines (27 loc) · 884 Bytes
/
Copy pathUserman2Example.py
File metadata and controls
35 lines (27 loc) · 884 Bytes
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
from functools import reduce
def get_int(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
print("Please enter a valid number!")
def get_nums():
while True:
n = get_int('Enter a whole number(0 to break): ')
if n <= 0: break
yield n
operations = {
'+': lambda x, y: x + y,
'-': lambda x, y: x - y,
'/': lambda x, y: x / y,
'*': lambda x, y: x * y,
'%': lambda x, y: x % y
}
while True:
oper = input(f'enter the operation you want to apply,\noperations: [{", ".join(operations.keys())}]\n>>> ')
if oper in operations:
break
else:
print('Please enter a correct operator!')
print('Enter all numbers you want to apply the operations to: \n')
print(f'the result of applying {oper} to the numbers is: {reduce(operations[oper], get_nums())}')