-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathcalc.py
40 lines (30 loc) · 1.07 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
#!/usr/bin/env python3
# Calculator
# Have the user enter 2 number - a. b -
# and an operator - op - and calculate
# the solution - c - according to the
# type of the given operator
def calc(a, b, op):
"""
Returns a string like this: a op b = c
where c is the computed value according to the opeartor
"""
if op not in '+-/*':
return 'Please only type one of these characters: "+, -, *, /"!'
if op == '+':
return(str(a) + ' ' + op + ' ' + str(b) + ' = ' + str(a + b))
if op == '-':
return(str(a) + ' ' + op + ' ' + str(b) + ' = ' + str(a - b))
if op == '*':
return(str(a) + ' ' + op + ' ' + str(b) + ' = ' + str(a * b))
if op == '/':
return(str(a) + ' ' + op + ' ' + str(b) + ' = ' + str(a / b))
def main(): # Wrapper function
a = int(input('Please type the first number: '))
b = int(input('Please type the second number: '))
op = input(
'What kind of operation would you like to do?\
\nChoose between "+, -, *, /" : ')
print(calc(a, b, op))
if __name__ == '__main__':
main()