Some help with Big If? MANY questions


#1





Okay I got through the exercise without any trouble up until now but at his point I have no idea what's going on: I don't understand what any of the bits and pieces of this code are referring to, and still have many unanswered questions, and regardless I just don't feel comfortable attempting this until I understand how it works. So my questions are.

In the case of the example for conditionals on the left

Why is  "this_might_be_true()" on the right of "if"? If "this_might_be_true()" is what?

Why does it say "print 'x' ", yet saying print when indented in this fashion (under "def x()" ) does not print anything in the console?

In General

 What does return mean? return what to what?

 Why did another person write "return the_flying_circus()" under else? What does returning "the flying circus" to "the flying circus mean"?

  What do the brackets in "the_flying_circus()" signify?


The following is the code which is given as an example for conditionals on the left. It gets a syntax error when I put it in, even when I add "def this_might_be_true()" above it.

if this_might_be_true():
    print "This really is true."
elif that_might_be_true():
    print "That is true."
else:
    print "None of the above."


Thanks in advance all!


#2

"what to where?" is more like it. But no real difference.

A call to a function will come from one scope, and the function has its own scope, so return is the bridge that allows data to flow back to the caller from one scope to the other.

[caller] --> (arguments) --> function(parameters) => return value/object => [caller]

Consider the following example.

def is_not_a_number(value):
    if type(value) == int: return False
    if type(value) == float: return False
    return True

The code inside the function is in its own scope. The calls below are higher up the scope chain, and cannot see the value variable inside the function. We pass the value as a literal, or as a cached value in a variable.

my_value = 42
print is_not_a_number(my_value)    # False

The function creates its own copy of my_value and stores it in value. The same will hold if we pass in a literal...

print is_not_a_number("some string")   # True

In each example above the # ____ is the return value for the given argument that was handed back up the scope chain to the caller.


#3

Given that it appears with () attached it is a function call to a function that is expected to return a value of True or False The return value is then evaluated and control flow passes through the if block when True, else it continues down the code to the elif conditional, This is a call to another function that is expected to return a boolean. If True then that code block is executed, otherwise flow continues to the else block, which code is executed by default.


#4

Hi @cosmo_kane ,

Taken literally, Codecademy's example for conditionals on the left that you posted is incomplete. It calls two functions that have not been defined, namely this_might_be_true and that_might_be_true.

Following is that example code again, but enhanced with definitions for those two functions. So that the functions can return different results at different times, a random number function, random.randint, is used. The two functions are written to inform you of what they are about to return, so that you can observe the effect that the results have on the control flow of the example code that Codecademy supplied.

You can copy the code and run it a few times as an experiment.

import random

def this_might_be_true():
    # Choose a random number, 1 or 2, and return True if it is 1.
    result = random.randint(1, 2) == 1
    print "this_might_be_true is about to return {:s}.".format(str(result))
    return result

def that_might_be_true():
    result = random.randint(1, 2) == 1
    # Choose a random number, 1 or 2, and return True if it is 1.
    print "that_might_be_true is about to return {:s}.".format(str(result))
    return result

# Call this_might_be_true() as the if condition
if this_might_be_true():
    print "This really is true."
# Call that_might_be_true() as the elif condition
elif that_might_be_true():
    print "That is true."
else:
    print "None of the above."

#5

Ah that explains a lot, I didn't know what was going on there! I'll download python so I can try that out. Thanks for the advice! That was a very quick response!


#6

Oh, okay. Starting to get to grips with this. It's quite a rush, this coding, I didn't expect it to be, but its really testing me in a fun kind of way. Thanks a ton for the advice!


#7

If you decide to run it in Python 3, make sure you enclose the output of all the print statements in parentheses, because print is a function in Python 3. For example, this ...

print "this_might_be_true is about to return {:s}.".format(str(result))

... should be changed to this ...

print("this_might_be_true is about to return {:s}.".format(str(result)))

Notice that we have to be careful to balance the parentheses when they are nested.


#8

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