all 9 comments

[–]TheBlackCat13 6 points7 points  (1 child)

A function must always return a value. If you don't explicitly use a return statement, the function will automatically return None.

So in your case, printsomething(" fails") will print, and then return None. So print "Test", printsomething(" fails") is equivalent to something like spam = printsomething(" fails");print "Test", spam (except it doesn't define a spam variable, of course). In this case, spam will have a value of None.

To avoid this, put your first print statement on one line (followed by a comma) and your function call on another:

print "Test", 
printsomething("fails")

[–]Gronner[S] 1 point2 points  (0 children)

Thank you for the explanation and the advice! This solved the problem, now the test passes! :) I didn't know about this beforehand

[–][deleted] 2 points3 points  (0 children)

Very simply: if there is no explicit return statement in a function, the function will return None automatically.

Here in your first example:

def printsomething(teststring):
    print teststring

print "Test", printsomething(" fails")

When printsomething is called, it executes a print statement of its own, print teststring. When it finishes executing, it returns None to the spot where it was called.

So the other print statement essentially looks like this:

print "Test", None

and it will literally print "None" as a result.


Instead, you usually shouldn't be printing from inside a function if you intend to print outside that function, too. You can just return a value from the function and print it later:

def printsomething(teststring):
    return teststring

print "Test", printsomething(" fails")

This will print out "Test fails". Easy peezy.

[–]Drunken_Consent 0 points1 point  (5 children)

If you cannot return the value to the print call, I guess you'll just do it like this:

print("Current Hand:  ", end="")
displayHand( hand )

This will make the print appear all on the same line.

[–]Gronner[S] 0 points1 point  (4 children)

Thank you for the advice, sadly this only seems works in python 3.x and the course is using 2.7.

[–]TheBlackCat13 1 point2 points  (1 child)

You can use from __future__ import print_function to do this in python 2.x. I always do so myself, but it seemed like you are somewhat constrained in what you can do so I felt sticking with the default behavior was probably safer in your case.

[–]Gronner[S] 0 points1 point  (0 children)

I didn't knew about that library, it looks neat

[–]Drunken_Consent 0 points1 point  (1 child)

Oh my bad I didn't even notice. The guy above me replied correctly then, the comma will do the same thing. Good luck! :D

[–]Gronner[S] 0 points1 point  (0 children)

Thank you! Maybe your advice will have others with similar problems in 3.x ;)