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.