Why does this code not print anything to the console?


#1



https://www.codecademy.com/en/courses/python-beginner-c7VZg/1/5?curriculum_id=4f89dab3d788890003000096#

I can get through the exercise with little difficulty, but I was playing around and realised that my code does not print to the console. Why is this so?

This is my code:

number = raw_input('Enter a number:')

def cube(number):
    result = number**3
    return result
    
def by_three(number):
    if number % 3 == 0:
        return cube(number)
        print "%s cubed is %s." % (number,result)
    
    else:
        return False
        print "This isn't divisible by 3."

All I get on the console is:

Enter a number: 8
None

Thank you!


#2

Hi, @umopapsidn ,

The raw_input function returns the user's input as a string. Therefore, you need to convert it to an int, so that you can do the math ...

number = int(raw_input('Enter a number:'))

A return statement terminates the execution of a function, so the print statements that you have do not get executed. Remove them from where they are, and print the result outside the functions ...

result = by_three(number)
if result:
    print "%s cubed is %s." % (number, result)
else:
    print "This isn't divisible by 3."

#3

hi @appylpye,

thanks for the reply :slight_smile:

I edited it a little, and this is what I got instead. I wanted to leave the print bits in the function because I only wanted things to be printed if the stated conditions are fulfilled first. That is alright, right? Anyway, I still got an error message :stuck_out_tongue:

Any further recommendations for me?
Thank you!


#4

@umopapsidn,
In your cube function you are accessing the global-variable result
instead that you returned number*number*number

Read the function-talk how to see and use the parameter in your functions!!

the FUNCTION talk

def myFunc( param1, param2):
    # Begin of =myFunc= FUNCTION-BODY
    # this =myFunc= function- has 2 PARAMETERS param1 and param2
    # param1 and param2 PARAMETERS are used 
    # as -local- VARIABLES throughout the =myFunc= FUNCTION-BODY
    print( param1 + " and " + param2 )
    #End of =myFunc= FUNCTION-BODY

If you want to call/execute the myFunc function
you will have to add a pair of parentheses to myFunc
like
myFunc()
As the myFunc function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren")

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++ function with 1 parameter using return-statement

def myFunction( param1 ):
    # //Begin of =myFunction= FUNCTION-BODY
    # //=myFunction= function has 1 PARAMETER param1
    # //this param1 PARAMETER is used as a -local- VARIABLE
    # //throughout the =myFunction= FUNCTION-BODY
    return param1;
    # //End of FUNCTION-BODY

You have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the =myFunction= FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 )

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the print-method to do a display.

theResult = myFunction( 4 )
print theResult

OR directly

print myFunction( 4 )

#5

thanks for the lengthy explanation on the formats of functions :slight_smile:

Sadly, I am still quite confused about a number of matters:

  1. What exactly is return? Is it just a keyword that allows the function to evaluate something?

  2. How do you access a variable instead of merely returning in? Or am I supposed to learn that in later units?

  3. Does this mean that functions can only evaluate arguments which are passed in via something like myFunc(4) but cannot evaluate a raw input decided spontaneously like the way I was trying to do?

Thanks so much, you're really helping me to learn :slight_smile:


#6

Hi @umopapsidn ,

A return statement, when executed, causes a function to cease execution and pass a value back to the statement that called the function.

Consider the built in len function ...

n = len("Monty")

The value of n is now 5. Although you don't see the internal workings of the len function, we do know that it returned the value 5, and that is why we are able to assign it to the variable, n. When you write a function, you can return a value, and use that value in a similar manner.

Functions can access global (external) variables, and can even be designed to alter their values. But, you should be careful about doing this, as it can easily lead to bugs that are difficult to find in a large program.

You have this ...

result = number**3

def cube(number):
    return result

If someone, such as Codecademy, does this, in an attempt to use your function ...

print cube(1)

... your function ignores the 1 that was passed to it, and instead accesses the global variable result, and returns its value.

For testing your function, Codecademy does not use the user input that was entered in response to the raw_input prompt. Instead it tests your function directly. It passed the value 1 to the cube function, after the user had entered 6. The function ignored the 1 that it was passed, and used the result of 216 instead by accessing the global value, result.

Consider what would happen if Python's len function was designed to access a global variable named my_string, and return the number of characters in a string that it referenced. Then, suppose you did this ...

my_string = "Spam"
print len("Monty")

Output ...

4

You would be confused and disappointed that the len function ignored the argument that you passed to it, and instead accessed the global variable, my_string.

Instead of depending upon the value of a global variable, result, move this statement inside your function ...

result = number**3

... so that you have this ...

def cube(number):
    result = number**3
    return result

Now, when someone calls your function, the number they pass to it as an argument is assigned to the parameter, number, and your function uses it to compute and return result.


#7

Hello,

thanks for helping to clarify the meaning of return for me :slight_smile:

Because of your help and @leonhard.wettengmx.n 's help, I have revised my code to get this:

which is a code that works :slight_smile:

But just some final questions:

  1. Why is it that 729 is printed as well? I thought only the print keyword will result in something being printed to the console, but not the return keyword.

  2. Why is None printed to the console as well even though I had a return statement for each of my functions?

  3. Why can't you set result instead of cube(number) in print "%s cubed is %s." % (number, cube(number)) ? Is it because result has become a local variable after I shifted it into the cube(number) function, and hence cannot be used in by_three(number)? If so, why was the error NameError: global name 'result' is not defined (ie, why is it still known as a global name)?

Thank you :slight_smile:


#8

Hi @umopapsidn ,

Why is it that 729 is printed as well? I thought only the print keyword will result in something being printed to the console, but not the return keyword.

Your by_three function contains print statements as well return statements. The print statements occur prior to the return statements in their respective blocks. Therefore, once the bug is in that function is corrected, the function will print some output, and then return a value. After that value is returned, you can also print it, with the following statement ...

print by_three(9)

Why is None printed to the console as well even though I had a return statement for each of my functions?

That particular None is just a result of Codecademy's doing some processing to test your code.

Why can't you set result instead of cube(number) in print "%s cubed is %s." % (number, cube(number)) ? Is it because result has become a local variable after I shifted it into the cube(number) function, and hence cannot be used in by_three(number)? If so, why was the error NameError: global name 'result' is not defined (ie, why is it still known as a global name)?

The name, result, is not global in the latest version of your code. It is only assigned a value in the cube function. When you attempt to access it in your by_three function, here, it is not recognized ...

print "%s cubed is %s." % (number, result)

The reason it should be changed to ...

print "%s cubed is %s." % (number, cube(number))

... is that the value you are seeking in the one that is returned by the cube function. Outside the cube function, it is not known by the name, result.