Python簡易計算機

轉載自網路

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()

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store