The Big If


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!


#Make sure that the_flying_circus() returns True
def the_flying_circus():
    if ________:    # Start coding here!
        # Don't forget to indent
        # the code inside this block!
    elif ________:
        # Keep going here.
        # You'll want to add the else statement, too!

when I last typed some code for this the answer i got was none, i'm unsure how to code to make it true, I am a total novice at coding i was taught some vba about 12 years ago?

the error message listed: Oops, try again. the_flying_circus() returned the value None, did you forget to add a return statement?

how do i make a return statement?

I thought it would print the word "failed" as in go directly to the else statement.

# Make sure that the_flying_circus() returns True
def the_flying_circus():
    if "yes" or "maybe" and 3<5:    # Start coding here!
     print("are you sure?")   # the code inside this block!
    elif "no":
     print("whyever not")    # Keep going here.
    else:
     print ("Failed")    # You'll want to add the else statement, too!

Appreciate any help or advice sorry for being a total moron :slight_smile:


#2

To make a return statement you use return like so:

def new_function(x):
    x += 1
    return x

The above will take a value, x, add one to it and return the new value of x.
print is different to return. print, prints stuff in the console but cannot be used by other stuff. return sends back a value to whatever called it. e.g. using new_function above:

>>> number = new_function(1)
>>> print number
2

>>> if new_function(1) == 2:
	   print new_function(1)
2

See how new_function returned the value 2 both times. But it didn't print 2 to the console until I used print. You obviously know what happens if print not return is used.

Now, why are you getting None? If a function does not execute a user made return statement it will return None.
e.g:

>>> def another_function(x):
            if x == 1:
                return True

>>> print another_function(1)
True

>>> print another_function(3)
None

As you can see, if x equals 1 then we hit my return statement.
But if x doesn't equal 1 it will not hit a return statement and so will return None by default.


#3

Thank you for the help! that made alot of difference the fact I didn't notice that I had to return the code apposed to print! thank you!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.