The Big If


#1

Well some how it work? I don't know how but i mess with the if, elif and else indentations and it worked.The thing is that i moved it back to where it was? Anyway i still like to know when or if i need to use parentheses for the Boolean operators and for Conditional statements do i need to print or return. In advance, thanks


#2

First off to solve the error you are getting. if elif and else should all be lined up, meaning they should all be the same number of spaces away from the edge. If you look closely at your code you will see the elif and else are about one or two spaces further away then if is.

As far as I am aware parentheses are only used with conditional statements when you are doing math operations and need to make sure certain equations are evaluated first.

With regards to printing over returning. When you use print it simply outputs to the console. When you use return the value is literally returned to whatever called it. So in your function you are returning true, if your conditional statement is true then it will return "True" to whatever called the function. If I were to run `print the_flying_circus() it would run the function and return the value "True" to the print statement which would then output "True" to the console.

Hopefully this all makes sense, if not let me know the confusion and I will try to explain it more.


#3

Firstly thanks for answering my question. For the Boolean operators if you need to use parentheses you use them right but like if answer = 5<6 or 2 > 1 i dont need to use them. For the Conditional statement can you explain more. i think i know what you saying but i still confused. Thanks


#4

Correct, answer = 5 < 6 or 2 > 1 does not need parentheses.

Here's an example of where you would need parentheses with a conditional statement. Let's say we want to take X and multiply it times 3 and see if it's greater than 10. We might be tempted to write the following function:

def example_function(x):
    if x * 7 - 4 > 10:
        return "It did something"
    else:
        return "It did not do something"
        
print example_function(15)

However, that condition statement will first multiply x * 7 and then subtract 4 and finally compare it to 10.

for it to do what we need it needs to be written as:

def example_function(x):
    if x * (7 - 4) > 10:
        return "It did something"
    else:
        return "It did not do something"
        
print example_function(15)

The parentheses tell it to first subtract 4 from 7 and the multiply and finally compare it to 10.

Obviously in that example we could've had just put in 3, but you will run into situations where it's important to have certain parts of the equation ran first.


#5

Hi there! I keep getting the error :Oops, try again. the_flying_circus() should return True, instead it returned: 'True'

My current code is: (with all the correct spacing)

Make sure that the_flying_circus() returns True

def the_flying_circus():
if (100/10) > 1: # Start coding here!
return 'True'
# Don't forget to indent
# the code inside this block!
elif answer == 10:
return "Stuck on the ground!"
# Keep going here.
# You'll want to add the else statement, too!
else:
return "No sir"

print the_flying_circus(100)

I am not sure why it is not working even though it comes out as true..


#6

Well i think if you tried
if (100/10) > 1
return True
that it would print True. Also you might want to indented you if and elif. Hopefully that helps


#7

What error does it give you?


#8

The error-
Oops, try again. the_flying_circus() should return True, instead it returned: 'True'


#9

Look closely at the return statement in your if clause. You will notice that True is surrounded by single quotes. That means the function will return True as a string instead of a bool. Remove the single quotes and you should be good to go.


#10

mine keeps saying "Oops, try again. the_flying_circus should not take any arguments.".

WHAT DOES THAT MEAN?!?!?!??!?!?!? please help


#11

When you are calling the function in your program make sure there is nothing between the parentheses.


#12

thank you so much!!!!!!!!!!


#13

def the_flying_circus():

if 5>4 and 7>5:    
    return True
    
elif 4>5 and 5>7:
    return False

I spent some time to figure out what is going on here.

I think the solution is quite simple. I didn't need to make an "else" statement as it was shown in the hint.


#14

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