0
How can I compare the result of a function with numbers?
This is my code: def func(×,y) res = x + y return = res print(func(float(input()),float(input())) if func < 10 print(Hello!) this is the error: TypeError: unorderable types: func() < int() Now I understand that I can't compare functions with numbers but I can't understand how to compare the result of the function with numbers. I tried like this: if func(x,y) < 10 print(hello!) but it says that x isn't defined. Can anybody help me with this? P.S.:I'm sorry for my bad english
4 Respostas
+ 3
You have several syntax errors:
def func(x, y): # missing colon
return x + y
if func(float(input()), float(input())) < 10: # missing colon
print("Hello!") # Hello! needs to be surrounded with quotes
+ 1
Thank you for your help!
+ 1
Actually, the error is caused by the fact that you are comparing a function NAME with a number instead of a function RESULT.
"func" is a function name, of type "function"
"func(10,10)" is a function result, of type... whatever you return (in this case, "int").
If you want this to work the way you wrote it, just store the function result in a variable.
myresult=func(float(input()),float(input()))
print(myresult)
if myresult<10:
print('Hello!')
Also, careful with ":" (it's required after "if" or "def"), and with string values (they should be within quotes - see "Hello!").
As for the error received when calling func(x,y), that is simply because you have no variable named "x". Try calling it with actual numbers: func(10,20)
0
thank you bogdan, that's a good tip.