I feel that this is a wrong question to ask because it is simply to wide, and as per my understanding, questions should always be very concise and precise.
But I have a lingering issue on the notion of function.
I feel that my problem is as follows:
When a function exist, and as a specific argument, how is python going to know how and when it should pass a variable to a function? My answer / understanding so far: Python will know because you will need to call the function. So the function is written somewhere, and then, you call the function by writing its name, plus the thing that you want to pass as parameter inside of it.
When several functions exist, how should they be organized? Is a something defined in a function going to impact another function? Or are they more or less "self contained", and hence a function could "seemingly" refer to exactly the same thing as another function, it is ok, because it is in a sense completely "hidden" from the other function? My answer / understanding so far: Functions can be written in whichever order, it does not matter. What matters is that then, they should be called in the right order afterwards. Functions can also refer to "overlapping" things (name etc...) it is ok, because they are all independent from each other.
I am trying to test on my own things, to finally resolve my blockage in understanding functions.
I have been trying to do the following below:
- A user gives an input (his name)
- A function takes it and tells to the user that he is a dog
- A function checks if the user is a dog, and writes something has a result
My first function works. But when using my second function I get this error for line 18:
TypeError: 'in <string>' requires string as left operand, not function
Here is my code:
x = input("give me your name")
def dog(value):
print(f"you are a dog, {value}")
def Anti_dog(blabla):
if dog in blabla:
print("Ho my goooood, he is a dog")
else:
print("everything is ok, he is a cat")
dog(x)
Anti_dog(x)
Following the comment, I tried to edit the code.
I tried doing two things.
First changing the print of the first function with a "return", but this did nothing.
Second, changing the dog into "dog", in order not to refer to the function dog, but to the presence of the string dog. I indeed do not have the old error, but the result is absurd, because the program is telling me that this is not a dog. Why?
Here is my code:
x = input("give me your name")
def dog(value):
return(f"you are a dog, {value}")
def Anti_dog(blabla):
if "dog" in blabla:
print("Ho my goooood, he is a dog")
else:
print("everything is ok, he is a cat")
dog(x)
Anti_dog(x)
I thought this would be really really simple as an exercise to "master" function, but this seems to become not as simple as I would have wished. I updated my code based on the answer below, but still get an error.
I tried really calling the function dog
in the function anti_dog
, as below:
x = input("give me your name")
def dog(value):
print(f"you are a dog, {value}")
def Anti_dog(blabla):
if "dog" in dog(x):
print("Ho my goooood, he is a dog")
else:
print("everything is ok, he is a cat")
dog(x)
Anti_dog(x)
My output is (with input "Luc" for "x"):
you are a dog, Luc you are a dog, Luc
Followed by the error:
TypeError: argument of type 'NoneType' is not iterable
I also deleted "dog(x)" at some point, because I felt that it may stupidily call the function twice (once as a stand alone, and a second time within "Anti_dog(x)"?), but I still have the error.
I am reading this to understand what caused the error, but I do not see how what I wrote would have caused this. I do not understand: my function returns something, and I have not set the value of my variable to "None".
After all these issues and comment here is a code that works (with the variable naming issue fixed):
x = input("give me your name: ")
def dog(value):
return(f"you are a dog, {value}")
def anti_dog(blabla):
if "dog" in dog(x):
print("Ho my goooood, he is a dog")
else:
print("everything is ok, he is a cat")
anti_dog(x)
dog
to print and not to return anything. If you don't return anything then you cant process the result. The error message is telling you the problem, NoneType ( what you are returning from callingdog(x)
) is not iterable.