The Big If... I dont understand


#1

This error keeps coming up:

File "python", line 7
elif 7 < 5 or 10 == 99:
^
SyntaxError: invalid syntax

I just need help undersanding what I did wrong

# Make sure that the_flying_circus() returns True
def the_flying_circus():
    if 1 == 1**2 and 10 == 5 + 5: # Start coding here!
        print "True"
    return True # Don't forget to indent
# the code inside this block!
    elif 7 < 5 or 10 == 99:
        print "False"
    return False
    # Keep going here.
# You'll want to add the else statement, too!
    else:
        print "False"
    return False


#2

Careful with your indentation - if you look at your return statements, when are they being executed? Are they executed if a statement is true/false, or are they executed regardless?

Don't forget that a return statement ends a function, so any code you are trying to execute afterwards is regarded as incorrect syntax.


#3

Could you help me as to HOW I should indent my code?
Because I thought I was doing it right, but clearly there's something I'm not understanding.
So if you could help me understand the indentation of the code then that would be great!


#4

Well as you know, a function's contents are signified by the indented code, like so:

def myFunction():
    # do stuff

The same can be said to be true for any code block, such as a conditional statement:

if x == 1:
    # do stuff

I have the feeling that you've grasped this concept, and the idea that you can place conditional statements inside functions (as this lesson requires) and indent as required.

The return statement ends a function and returns its output. If you want your function to return a result based on a condition, then the return statement has to be indented so that it lies inside the conditional statement, like so:

def myFunction:
    if x == 1:
        return "foo"
    else:
        return "bar"

If your return statement is outside the conditional statement (as in your code), it is executed regardless as there is nothing to halt the function.


#5

Ok!
Thanks a billion!!!
I was able to finish the lesson!


#6

Sorry, I'm stuck at the same point... so i tried to get ideas from jeremiah's one
this is my code

def the_flying_circus (s):
    if s == "Yes" or s == "yes" or s == "YES":
        return True
    elif s == "No" or s == "no" or s == "NO":
        return False
    else:
        return False
print the_flying_circus("YES")

the exercise says that the code must return true when evaluated and that's what happens but...

the answer is : Oops, try again. the_flying_circus should not take any arguments.

I don't understand what is going wrong....
Sorry I'm a very beginner :frowning:


#7

Sorry - I hadn't noticed your reply until now!

Your code is technically correct, but the lesson is requiring that you not use any arguments in your function.

def the_flying_circus(s): <-- In this case, s is your argument

A quick fix to circumvent this would be to have s defined inside the function:

def the_flying_circus():
    s = "YES"
    # stuff happens
print the_flying_circus()

Not ideal in any form, but it passes - at least you understand how arguments work!


#8

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