0

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441

3 Answers3

1

it does all of those, except print the result

The simplest answer is that it's because you didn't tell it to. Python only prints things out that you tell it to when you write print(<something>) in your code.

When you write add(num1, num2), it is computing the result, but then you aren't doing anything with that result. You could, for instance, do this:

answer = add(num1, num2)
print(answer)

This declares a variable to store the result, so that it isn't lost. And then prints it.


Beyond the immediate question you have about printing the result, you will find that the value you get from input() is a string, not a number. Your add() function will do num1 + num2 but since these are strings, it is actually concatenating (joining) them, like "3" + "4" = "34" which is not what you want.

You should make be sure to convert your inputs to numbers with int() or float(). And I would recommend not having the str() call inside add(). The print() function can print numbers just fine.

rgov
  • 3,516
  • 1
  • 31
  • 51
0
num1 = input("What is number 1? ")

input() returns a string, so you need to convert both inputs to int()s so they can have mathematical operations performed.

clubby789
  • 2,543
  • 4
  • 16
  • 32
  • If this were the problem the OP would have an error or exception, whereas they're only reporting that they don't have any a result printed. (This implies they're on Python 2, where `input()` implicitly does an `eval()`, and *does not* return a string). – Charles Duffy Aug 03 '19 at 21:33
  • Hmm. They *could* be on Python 3 and just getting *inaccurate* results (`"3" + "4" == "34"`), but even then, this doesn't address the question they *actually asked*, which is about not having any output. – Charles Duffy Aug 03 '19 at 21:34
0

Here is a working example of your calculator:

def add(num1, num2):
    return num1+num2

def sub(num1, num2):
    return num1-num

def mul(num1, num2):
    return num1*num2

def div(num1, num2):
    return num1/num2

def main():
    operation = raw_input("What operation would you like to perform? add, sub, mul or div: ")
    num1 = float(input("What is number 1? "))
    num2 = float(input("What is number 2? "))
    if (operation == 'add'):
        print(add(num1, num2))
    elif (operation == 'sub'):
        print(sub(num1, num2))
    elif (operation == 'mul'):
        print(mul(num1, num2))
    elif (operation == 'div'):
        print(div(num1, num2))
    else:
        print("Error: no such operation")

main()

Note: for your operation, you have to use raw_input instead of input.

Gilfoyle
  • 3,282
  • 3
  • 47
  • 83
  • You may be getting confused between python 2 and python 3. In python 3, (which is what I assume Inigo Burrell will be using), raw_input() doesn't exist. Whereas in python 2 it does. Therefore Inigo should use input instead. However if Inigo is using Python 2, then raw_input() would be fine. – Sam Aug 03 '19 at 21:07
  • In Python 3, `input()` is correct, and there is no need to cast `num1` and `num2` to floats before dividing, because the `/` operator always performs floating point division. – rgov Aug 03 '19 at 21:16
  • 4.2 + 7.915 will not work here because of the int conversion :( – Daniel Krom Aug 03 '19 at 21:21