-2

I have this code:

def the_flying_circus():
    if True and True and True and not False:
        print "Kevin stinkt"
    elif 10 < 4:
        print "Justin stinkt"
    else:
        print "herb-wuerzig"

When I print the_flying_circus I get Kevin stinkt printed, None as a return. I would need False as return for an online tutorial. Why do I get None, and how can I achieve an True?

bngschmnd
  • 111
  • 1
  • 10
  • 3
    *"I get Kevin stinkt as a return"* - **incorrect**. The function *prints* `'Kevin stinkt'` and *returns* `None`. See e.g. http://stackoverflow.com/q/7664779/3001761. – jonrsharpe Oct 27 '14 at 22:39

3 Answers3

2

None is the return value of the function. A function that finishes without an explicit return statement will return None.

In response to your additional question:

If you want the function to return true, put

return True

at the end. If you want it to return false, put

return False

at the end.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • okay, i see the difference. how do i get the function to return true (false)? – bngschmnd Oct 27 '14 at 22:41
  • @bngschmnd: Exactly like it sounds: `return True`. – abarnert Oct 27 '14 at 22:42
  • 1
    @bngschmnd: But why do you want to return True anyway? What are you going to do with that value? That'll just meant that when you type `the_flying_circus()` in the interpreter, your function will print out `Kevin stinkt`, and then the interpreter will print out `True` underneath that. – abarnert Oct 27 '14 at 22:43
  • i need that for an online python tutorial `# Make sure that the_flying_circus() returns True` – bngschmnd Oct 27 '14 at 22:46
1

The function returns None if it does not return anything else, hence you first print inside the function and then you print the None that was returned.

If you either exchange your print statements with return or just call the_flying_circus() instead of print the_flying_circus() you will get the expected result.

def the_flying_circus():
    if True and True and True and not False:
        return "Kevin stinkt"
    elif 10 < 4:
        return "Justin stinkt"
    else:
        return "herb-wuerzig"

Then you can run the function and print the returned value:

print the_flying_circus()

Or you can do:

def the_flying_circus():
    if True and True and True and not False:
        print "Kevin stinkt"
    elif 10 < 4:
        print "Justin stinkt"
    else:
        print "herb-wuerzig"

And just call the function without printing the returned value:

the_flying_circus()
Enfenion
  • 502
  • 3
  • 8
  • tried `#Make sure that the_flying_circus() returns True def the_flying_circus(): if True and True and True and not False: return true elif 5 < 10: return true else: return true` and `# Make sure that the_flying_circus() returns True def the_flying_circus(): if True and True and True and not False: return true elif 5 < 10: return true else: return false` - didnt seem to work at all? – bngschmnd Oct 27 '14 at 22:51
  • It is very difficult to read without indentation, but at least you need to use `True` and `False` with a capital first letter. – Enfenion Oct 27 '14 at 22:57
0

The code needed is the following:

# Make sure that the_flying_circus() returns True def the_flying_circus(antwort): if antwort > 5:
print "H" elif antwort < 5: print "A" else: print "I" return True

what ever input I give, the_flying_circus always returns True

bngschmnd
  • 111
  • 1
  • 10