Home > Software design >  Python crash course Exercise 8-11: Please clarify how the return statement works
Python crash course Exercise 8-11: Please clarify how the return statement works

Time:01-10

I’m going through the book “Python Crash course”, and I have arrived at an exercise on passing a copy of a list as an argument to a function. I could neither solve the problem, nor is the solution offered by the author is fully clear to me.

This is the exercise text:
Unchanged Magicians: Start with your work from Exercise 8-10 . Call the function make_great() with a copy of the list of magicians’ names . Because the original list will be unchanged, return the new list and store it in a separate list . Call show_magicians() with each list to show that you have one list of the original names and one list with the Great added to each magician’s name.“

My problem is really with the highlighted line. Why is the return statement needed?
I mean what happens actually once the return magicians statement in the code below is executed?

Could you also please explain to me the error I got when I commented out the return statement: TypeError: 'NoneType' object is not iterable?

def show_magicians(magicians):
    """Print the name of each magician in the list."""
    for magician in magicians:
        print(magician)

def make_great(magicians):
    """Add 'the Great!' to each magician's name."""
    # Build a new list to hold the great musicians.
    great_magicians = []

    # Make each magician great, and add it to great_magicians.
    while magicians:
        magician = magicians.pop()
        great_magician = magician   ' the Great'
        great_magicians.append(great_magician)

   # Add the great magicians back into magicians.
   for great_magician in great_magicians:
       magicians.append(great_magician)

   return magicians

magicians = ['Harry Houdini', 'David Blaine', 'Teller']
show_magicians(magicians)

print("\nGreat magicians:")
great_magicians = make_great(magicians[:])
show_magicians(great_magicians)

print("\nOriginal magicians:")
show_magicians(magicians)

Thank you so much in advance!

CodePudding user response:

Return statements is needed because you want to store the result of make_great which is list of magicians.

great_magicians = make_great(magicians[:])

If you do not provide return statement None will be returned. So than you can not iterate through None in show_magicians.

# without return statement great_magicians will be None
great_magicians = make_great(magicians[:])
show_magicians(great_magicians)

CodePudding user response:

first of let me explain how return statement works, the function continues execution until there is return statement. the return statement is used to end the execution of a function and it returns the values to its caller, the code after return statement is not executed.

for example consider following code.

def get_full_name(first_name, last_name):
    """the function takes first_name and last_name as parameter the concatenates both values and returns the full name"""
    return first_name   " "   last_name
    # if there's further code after the return statement it won't execute.

full_name = get_full_name("Ali", "Muhammad") 
# the value returned by get_full_name function will be assigned to full_name variable. 

BTW your code looks good! maybe there is indentation at

for great_magician in great_magicians:
    magicians.append(great_magician)
    return magicians

you can check working code here: https://colab.research.google.com/drive/1Ie9Nd9jlgKW5JTx_ZQdAkkEwykarDfv_?usp=sharing

  •  Tags:  
  • Related