Why is it that when this code is executed, I would get 'hi'?
Thanks!
def b():
print("hi")
def c():
return True
if b() == 'hi':
print("Done")
You are confusing printing to the console with returning a value. Your function implicitly returns None if you do not return anything from it so it is never equal to 'hi'. Your b() does print - and not return its 'hi'
def b():
print("hi") # maybe uncomment it if you do not want to print it here
return "hi" # fix like this (which makes not much sense but well :o)
def c():
return True
if b() == 'hi':
print("Done")
You can test it like this:
def test():
pass
print(test())
Outputs:
None
Further readings:
about the return statement
about defining functions (read the paragraph below the second fib-CodeBlock example - it tells you about None)
One even more important thing to read: How to debug small programs (#1) - it gives you tips on how to fix code yourself and find errors by debugging.
Essentially what you're doing is saying if b(), which runs the b() function and prints "hi" is equal to "hi", print "done", but since your function prints "hi", rather than returning "hi", it will never equal true.
Try this:
def b():
return "hi"
def c():
return True
if b() == 'hi':
print("Done")
Related
I am learning some new topics in python, and I finally got to decorators, which seems cool and all except theres one major issue. Below is the code I have running:
def new_dec(ori_func):
def WrapFunc():
print("First Line")
ori_func()
print("Second line")
return WrapFunc
def new_func():
print("This is the new function")
ThisFunc = new_dec(new_func)
print(ThisFunc())
However, when this code is executed it outputs:
First Line
This is the new function
Second line
None
And I do not remember adding a None statement, is this possibly a type variable that has been added? Why is this happening, and how can this be fixed.
You have four print statements. So four things get printed. And by default a function returns None:
>>> def f(): pass
>>> print(f())
None
Check what happens if you define:
def new_dec(ori_func):
def WrapFunc():
print("First Line")
ori_func()
print("Second line")
return "Fourth Line"
return WrapFunc
Alternatively you can remove the fourth print:
# print(ThisFunc())
ThisFunc()
By default, a function returns None. You just have to call the function to get what you expect.
def new_dec(ori_func):
def WrapFunc():
print("First Line")
ori_func()
print("Second line")
return WrapFunc
def new_func():
print("This is the new function")
ThisFunc = new_dec(new_func)
ThisFunc() # get rid of the print statement
When there is a print statement inside a function, it is not printing.
I dont understand what is going wrong.
def test():
print("please print this")
return "return this"
And, my main function is like this:
if __name__ == "__main__":
classwhereiwrotefunction.test()
When I use a debugger, and try to store the return value in a variable, it does show the value. But, does not print it.
the problem is with indentation I guess. Try the below code it will work.
class Class_name:
def test(self):
print("please print this")
return "return this"
if __name__ == "__main__":
class_name = Class_name()
print(class_name.test())
>> please print this
>> return this
I have a class file. Let's call it "C1.py". The sample code looks like below.
class C1(object):
def __init__(self):
self.greeting = "Hello, world!"
def M1(ans):
if ans == 1 or ans == 2:
return True
else:
return False
Now, I have another python file in the same folder, which will access the class file shown above.
from trial import C1
def getAns(class1):
while True:
ans = input("Answer: ")
if class1.M1(ans):
return ans
break
sample = C1()
print sample.greeting
ans = getAns(sample)
print ans
When I run those files, sample.greeting prints fine. Everything is fine until when the execution reaches the line "ans = getAns(C1)", which gives the error "M1() takes exactly 1 argument (2 given)".
So, where in the code should I change so that I can call that method successfully?
Note here that the above code is only the abstraction of my whole program to highlight my problem. It sounds stupid with just the code above alone. So, please, please bear with that for me.
M1 is currently defined as a method of C1, as such it needs to have an additional self argument which would be the instance of the class. I.e
class C1(object):
def __init__(self):
self.greeting = "Hello, world!"
def M1(self, ans):
if ans == 1 or ans == 2:
return True
else:
return False
In other languages such as C++ or Java the presence of self (or this) is implicit, but in python it's explicit.
alternatively if you don't want or need M1 to access C1's state you could make M1 static via #staticmethod i.e.
class C1(object):
def __init__(self):
self.greeting = "Hello, world!"
#staticmethod
def M1(ans):
if ans == 1 or ans == 2:
return True
else:
return False
def other_new_function():
print "HELLO"
print "Start", other_new_function(), "Stop"
The output of this program is:
Start HELLO
None Stop
Why is NONE being showed in the output?
Because you're printing "HELLO", and not returning it.
Because your function does not return anything, it defaults to returning None. Hence, the None that appears.
Change:
def other_new_function():
print "HELLO"
To:
def other_new_function():
return "HELLO"
There's a big difference to returning and printing something from a function:
>>> def func():
... print 1
...
>>> def func2():
... return 1
...
>>> myvar = func()
1 # Printed
>>> myvar2 = func2() # Returned to a variable
>>> print myvar
None
>>> print myvar2
1
Because function 1 prints the value, it never gets returned to a variable.
def other_new_function():
print "HELLO"
returns NoneType (which gets printed as 'None') since you dont explicitly return a value. What you probably want to do is:
def other_new_function():
return "HELLO"
for example i have def Hello():
and here is the code
def Hello():
F = 'Y'
if F == 'Y':
#here i want get out of the Hello() to Hey()! by how!
To exit the 'Hello' function:
def Hello():
F = 'Y'
if F == 'Y':
return
You can use 'return' to exit a function before the end (though there is a school of thought that frowns on this, as it makes it slightly harder to form a solid picture of the execution flow).
This will go on to the 'Hey' function if you called it with e.g.:
Hello()
Hey()
Or, to 'jump' to the 'Hey' function, use:
def Hello():
F = 'Y'
if F == 'Y':
Hey()
...but this means the call stack will still contain the data for the 'Hello' function - so when you return from the 'Hey' function, you will be returning to within the 'Hello' function, and then out of that.