forked from codehouseindia/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator
More file actions
77 lines (56 loc) · 2.06 KB
/
Calculator
File metadata and controls
77 lines (56 loc) · 2.06 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
65
66
67
68
69
70
71
72
73
74
#https://www.facebook.com/baibhav.tripathy.5/posts/346793770081669
#Subscribed to Code House
# define a dictionary which has functions for the operations
operations = {
'+' : lambda number_1, number_2: number_1 + number_2,
'-' : lambda number_1, number_2: number_1 - number_2,
'*' : lambda number_1, number_2: number_1 * number_2,
'/' : lambda number_1, number_2: number_1 / number_2,
}
def input_number(prompt='enter a number: '):
'''the user must enter a number'''
try:
return int(input(prompt))
except ValueError:
print('Not got a number')
exit()
def calculation_error(*arguments):
'''in case operator is not supported then inform the user'''
print('You have not typed a valid operator, please run the program again.')
# driver code
operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division:
''') or None
number_1 = input_number('Enter your first number: ')
number_2 = input_number('Enter your second number: ')
# perform the calculation and store the result otherwise display thew error
result = operations.get(operation, calculation_error)(number_1, number_2)
print(f'{number_1} {operation} {number_2} = {result}')
# operation = input('''
# Please type in the math operation you would like to complete:
# + for addition
# - for subtraction
# * for multiplication
# / for division
# ''')
# number_1 = int(input('Enter your first number: '))
# number_2 = int(input('Enter your second number: '))
# if operation == '+':
# print('{} + {} = '.format(number_1, number_2))
# print(number_1 + number_2)
# elif operation == '-':
# print('{} - {} = '.format(number_1, number_2))
# print(number_1 - number_2)
#
# elif operation == '*':
# print('{} * {} = '.format(number_1, number_2))
# print(number_1 * number_2)
# elif operation == '/':
# print('{} / {} = '.format(number_1, number_2))
# print(number_1 / number_2)
# else:
# print('You have not typed a valid operator, please run the program again.')