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?
CodePudding user response:
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))
CodePudding user response:
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)
CodePudding user response:
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))
CodePudding user response:
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))