-1
num1 = float(input("Enter first number: "))
op = input("Enter operator: ")
num2 = float(input("Enter second number: "))

def cal(num1, num2):
    if op == "*":
        print(num1*num2)
    elif op == "/":
        print(num1/num2)
    elif op == "-":
        print(num1-num2)
    elif op == "+":
        print(num1+num2)
    else:
        print("Error 404")

print(cal(num1, num2))

this code runs right but says "none" at the end why?

DrBwts
  • 3,470
  • 6
  • 38
  • 62
  • 2
    Your `cal` doesn't return a value, it just prints out some things. Therefore, trying to use `print` on it will result in `None` being printed. – Bob th Aug 04 '22 at 18:41
  • Does this answer your question? [What causes my function to return None at the end?](https://stackoverflow.com/questions/21471876/what-causes-my-function-to-return-none-at-the-end) – Pranav Hosangadi Aug 04 '22 at 19:00

4 Answers4

0

Change your print statements in function to return statements, otherwise the function will be implemented, and it will print the result in console, but at your print statement you will get none. because - when you use "Print" - you simply print a value in the console. When you use "Return" - you get a value from a function.

num1 = float(input("Enter first number: "))
op = input("Enter operator: ")
num2 = float(input("Enter second number: "))

def cal(num1, num2):
    if op == "*":
        return(num1*num2)
    elif op == "/":
        return(num1/num2)
    elif op == "-":
        return(num1-num2)
    elif op == "+":
        return(num1+num2)
    else:
        return("Error 404")

print(cal(num1, num2))
0

Your code actually works. The problem with your code is that when you call calc() inside the print function, it tries to look for value that might be provided within the calc function (via the return statement). Since you did not return any value within your function, the interpreter cannot find the correct "value" to print. Note that none is a data type in Python. To solve the problem you can just change the print statements inside the if...else statement to a return statement. This way, when the operator is provided by the user, it will compute the result and return the result to the calc()'s function caller. Here is the complete version of what you are trying to achieve:

num1 = float(input("Enter first number: "))
op = input("Enter operator: ")
num2 = float(input("Enter second number: "))

def cal(num1, num2):
 if op == "":
  input("Nothing entered. Please enter an operator. ")
 elif op == "/":
  return num1/num2
 elif op == "-":
  return num1-num2
 elif op == "+":
  return num1+num2
 else: 
  return "Error 404")

print(cal(num1, num2))
Toby Harnish
  • 90
  • 1
  • 9
  • 1. Python functions implicitly return `None`, so _"Since you did not return any value within your function, the interpreter cannot find the correct "value" to print"_ is incorrect. 2. `None` is not a data type. 3. What's the point of the `if` inside the function? If that condition is satisfied, nothing will be returned. Anyway, this violates the [single-responsibility principle](https://en.wikipedia.org/wiki/Single-responsibility_principle) of good software design – Pranav Hosangadi Aug 04 '22 at 19:05
  • @Pranav Hosangadi, I'm just trying to help. I am a new Python learner too. Regarding to your other statement, "None is not a datatype. " -- see https://www.quora.com/What-is-None-data-type-in-Python. – Toby Harnish Aug 04 '22 at 19:50
  • That link doesn't say `None` is a data type. `NoneType` is the type, and it's a "[singleton class](https://en.wikipedia.org/wiki/Singleton_pattern)", which allows you to have only one object of this type, which is the `None` object. – Pranav Hosangadi Aug 04 '22 at 19:57
0

As you have not provided a value for your function to return the Python interpreter returns a value for you. So when your print() calls your function cal the function returns nothing or put another way returns a value of None and that's what your print() statement prints.

Compare your code with the following where a value is returned to the print() statement,

num1 = float(input("Enter first number: "))
op = input("Enter operator: ")
num2 = float(input("Enter second number: "))

def cal(num1, num2, op):
    
    if op == "*":
        cal_val = num1 * num2
    elif op == "/":
        cal_val = num1 / num2
    elif op == "-":
        cal_val = num1-num2
    elif op == "+":
        cal_val = num1 + num2
    else:
        cal_val = "Error 404"
    
    return cal_val

print(cal(num1, num2, op))

Alternatively you could leave your code as it is and just call your cal() function without the print() wrapped around it. As below,

num1 = float(input("Enter first number: "))
op = input("Enter operator: ")
num2 = float(input("Enter second number: "))

def cal(num1, num2):
    if op == "*":
        print(num1*num2)
    elif op == "/":
        print(num1/num2)
    elif op == "-":
        print(num1-num2)
    elif op == "+":
        print(num1+num2)
    else:
        print("Error 404")

cal(num1, num2)
DrBwts
  • 3,470
  • 6
  • 38
  • 62
-1

Try this instead

num1 = float(input("Enter first number: "))
op = input("Enter operator: ")
num2 = float(input("Enter second number: "))

def cal(num1, num2):
    if op == "*":
        return num1*num2
    elif op == "/":
        return num1/num2
    elif op == "-":
        return num1-num2
    elif op == "+":
        return num1+num2
    else:
        return "Error 404"

print(cal(num1, num2))
Valerii
  • 114
  • 5