Question about Practice Makes Perfect example


#1



In the "Practice Makes Perfect" lesson in the functions lesson set I was interested in the example they gave. I copied it into a python IDE and added the last line of code just to play with functions a bit more and try to get a deeper understanding of the return function.


when I run the program as displayed in the example it returns NONE, as expected.

When I add the last line with the print I get the following error:

Traceback (most recent call last):
File "python", line 9, in
NameError: name 'phrase' is not defined


I expected it to print YOU'RE SHOUTING! as that should be what was returned to the phrase variable by the shout function when it was called with I'M INTERESTED IN SHOUTING as the phrase variable.


def shout(phrase):
    if phrase == phrase.upper():
        return "YOU'RE SHOUTING!"
    else:
        return "Can you speak up?"

shout("I'M INTERESTED IN SHOUTING")

print(phrase)

I also tried setting it up a different way since I thought maybe it would work if the phrase variable was defined before the function so I tried the following code expecting to be able to get both results(YOU'RE SHOUTING! and Can you speak up?) depending on the input. Instead it just returns the input string as if the function never happened.

phrase = raw_input("You got something to say?? ")

def shout(phrase):
    if phrase == phrase.upper():
        return "YOU'RE SHOUTING!"
    else:
        return "Can you speak up?"

shout(phrase)

print(phrase)

I guess I'm mostly confused about how return is working here. I realize that this is just supposed to be an example but it's really got me stupped about things I thought I understood here. Thanks to anyone who can shed some light on this.


#2

phrase is a placeholder for the input to your shout function. You can not print(phrase) because you can only access it inside the shout function. To print the result of calling shout on "I'M INTERESTED IN SHOUTING" use

print(shout("I'M INTERESTED IN SHOUTING"))

This will print the value the shout function returns.


#3

Thank you very much. I also realize now that in the second example I tried if I just change the last lines to the following code it works fine:

print shout(phrase)

Although it gets less confusing if I change the variables around as follows:

user_phrase = raw_input("You got something to say?? ")

def shout(phrase):
    if phrase == phrase.upper():
        return "YOU'RE SHOUTING!"
    else:
        return "Can you speak up?"

print shout(user_phrase)

I understand it much better now. Thanks again!


#4

I completly overlook your second example.

The difference is that in the first line of the second example you defined phrase as a global variable.
You also uses phrase as the parameter name to the shout function. You can reuse the name of a global variable as a function parameter. Inside the function the name will then refer to the parameter not the function.
So for your second example

The code is equivalent to:

phrase = raw_input("You got something to say?? ")

def shout(some_input):
    if some_input == some_input.upper():
        return "YOU'RE SHOUTING!"
    else:
        return "Can you speak up?"

shout(phrase) # this doesn't print anything

print(phrase) # this prints phrase but is not using the shout function at all

#5

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