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)