I am a beginner with Python and am trying to write my first calculator program. However, the test that I set up for it did not work. It asked me for the operation and the numbers, but it didn't print the result. Why not?
I have tried rewriting it in different ways, but it still doesn't work.
def add(num1, num2):
print(str(num1+num2))
def sub(num1, num2):
return str(num1-num2)
def mul(num1, num2):
return str(num1*num2)
def div(num1, num2):
return str(float(num1)/float(num2))
def main():
operation = input("What operation would you like to perform? add, sub, mul or div: ")
num1 = input("What is number 1? ")
num2 = input("What is number 2? ")
if (operation == 'add'):
add(num1, num2)
main()
I expected it to ask what the operation I wanted to perform was, then to ask what the numbers were, and then to print the result. Instead, it does all of those, except print the result. Please could somebody point out where I have gone wrong. NB: I only put in a case for 'add' because I was just testing it.