0

I am creating a function called show_magicians() that prints out the content of a list called magician_names.

I am trying to create a new function called make_great() that will modify the list magician_names into saying "The Great [Insert name]".

However I can only get it to print out "The Great Nick" and I am not sure how to modify this list so that when I call show_magicians, the function will print out the modified list.

For example:

magician_names = ['jim', 'kaitlyn', 'nick', 'paul', 'kyle']

After make_great function:

magician_names = ['The Great jim', 'The Great kaitlyn', 'The Great nick', 'The Great paul', 'The Great kyle']

Below is the code I have so far:

def show_magicians(magician_names):
    for magician_name in magician_names:
        print(magician_name.title())


magician_names = ['jim', 'kaitlyn', 'nick', 'paul', 'kyle']

show_magicians(magician_names)

def make_great(magician_names):
    for magician_name in magician_names:
        print("The Great " + magician_name)


make_great(magician_names)
4
  • Try highlighting your code and press ctrl+k to indent it. This way it will be readable Commented Jan 22, 2018 at 2:18
  • My code is indented I just copied and pasted my code into here. I guess it didn't keep the same format. Sorry for the messy question. Commented Jan 22, 2018 at 2:20
  • Your code wasn't indented. Code blocks in Markdown (which is what StackOverflow uses) need to be indented 4 spaces. Commented Jan 22, 2018 at 2:21
  • Yep, much better! Commented Jan 22, 2018 at 2:23

4 Answers 4

1

I structured the code like this:

def show_magicians(magician_names):
    for magician_name in magician_names:
        print(magician_name.title())

def make_great(magician_names):
    for magician_name in magician_names:
        print("The Great " + magician_name)

magician_names = ['jim', 'kaitlyn', 'nick', 'paul', 'kyle']

show_magicians(magician_names)
make_great(magician_names)

Output:

Jim
Kaitlyn
Nick
Paul
Kyle
The Great jim
The Great kaitlyn
The Great nick
The Great paul
The Great kyle

I should point out that the def make_great(magician_names): doesn't create a new list and return it, it takes a list as a parameter, in this case, magician_names loops over it and adds the phrase "The Great...".

The author does have answers to his excerises: https://ehmatthes.github.io/pcc/

I have this book as well, however, I didn't encounter the problems you have.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I didn't realize he had the solutions posted.
0

You're on the right track, but the basic approach is for your function to 1) make an empty list; 2) for each entry in the passed-in list, create your modified name; and 3) add that modified name to the (once-empty) list you had created. Then pass back that list.

Example:

def make_great(magician_list):
    great_list=[]
    for name in magician_list:
        newname = "The Great " + name
        great_list.append(newname)

    return great_list

magician_names = ['jim', 'kaitlyn', 'nick', 'paul', 'kyle']
print magician_names
great_magician_names = make_great(magician_names)
print great_magician_names

2 Comments

Thank you for the help. This made a lot of sense. It wasn't as clear in the book that I am following.
My answer actually leaves you with a new list. Since your question said "modify the list" if you actually wanted to do that, throwing away the old list, you could just specify "magician_names = make_great(magician_names)" The list comprehension approach is much nicer approach once you've got the basics under your belt, and you should still keep that approach in mind. It won't be long until you're comfortable with it: great_magician_names = ["The Great " + name for name in magician_names]
0

Instead of a function, why not use a list comprehension?:

magician_names = ['jim', 'kaitlyn', 'nick', 'paul', 'kyle']
magician_names = ["The Great {}".format(i) for i in magician_names]

1 Comment

I am following a book. Called Python crash Course. I'm in the functions section and this is one of the try it yourself examples. However there is no solutions example
0

What about this ?

magician_names = ['jim', 'kaitlyn', 'nick', 'paul', 'kyle']

print(list(map(lambda x:"The Great {}".format(x),magician_names)))

output:

['The Great jim', 'The Great kaitlyn', 'The Great nick', 'The Great paul', 'The Great kyle']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.