0

How to write code for it?

Write a function make_greate() that modifies the list of magicians by adding phrase the Great to each Magician's name.call show_magicians() to see that the list has actually been modified. https://code.sololearn.com/c4w7nSoXy5oN/?ref=app

16th Dec 2020, 8:31 AM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
23 Respostas
+ 6
Try this: def make_great(magicians): magicians=[j.replace(j,"Great "+j) for j in magicians] return magicians def show_magicians(magicians): for i in magicians: print(i) magicians = ["David","Doug Henning","Harry Anderson"] magicians=make_great(magicians) show_magicians(magicians)
16th Dec 2020, 9:10 AM
R_A
+ 4
FIXED (same method) def make_great(megicians): for i in range(len(megicians)): megician = megicians.pop() megician1 = "Great " + megician megicians.insert(i, megician1) def show_magicians(megicians): print(i) magicians = ["David","Doug Henning","Harry Anderson"] make_great(magicians) show_magicians(magicians) #And also the variables used inside the function must be the Parameter, not the Argument itself.
16th Dec 2020, 9:08 AM
noteve
noteve - avatar
+ 4
🎶💞Sravs💞🥀 Yes sure. It takes every element of the list "magicians" (which is ["David", "Doug Henning" ,"Harry Anderson"]) one by one which are strings , and replace that particular string with -"Great "+string- and store them inside the new list which is also named as "magicians". Hope you understand. :-)
16th Dec 2020, 4:55 PM
R_A
+ 3
🎶💞Sravs💞🥀 Is this what you do not understand? String.replace(old substring to be replaced, new substring which would replace the old substring)
17th Dec 2020, 5:03 AM
R_A
+ 3
🎶💞Sravs💞🥀 We can write for loops or while loops or conditions (like if condition) in a single line like that. It's normally used to shorten the code. It's like, List=[do this for i in another list]
17th Dec 2020, 5:26 AM
R_A
+ 3
Because string.replace(old,new) returns just the new string and it doesn't replace the old string in the list for us . We have to store it by ourselves.
17th Dec 2020, 5:29 AM
R_A
+ 3
🎶💞Sravs💞🥀 We don't change the list here. Here .replace() just takes an element of a list, to outside, and replace that element with another one , and return that new string, so that particular element is not at all changed inside the list.
17th Dec 2020, 5:35 AM
R_A
+ 2
Analyze this code, hope it will help you :) https://code.sololearn.com/cTQOH0eJclS5/?ref=app
18th Dec 2020, 3:35 AM
LONEWOLF
LONEWOLF - avatar
0
FIXED (same method) def make_great(megicians): for i in range(len(megicians)): megician = megicians.pop() megician1 = "Great " + megician megicians.insert(i, megician1) def show_magicians(megicians): for i in megicians: yield i magicians = ["David","Doug Henning","Harry Anderson"] make_great(magicians) print(list(show_magicians(magicians))) #And also the variables used inside the function must be the Parameter, not the Argument itself. This is printing list in reverse order
16th Dec 2020, 3:53 PM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
0
Oh yeah I did not notice. Just replace the "i" with "0" in the megicians.insert (line 5) #Should be like this: megicians.insert(0, megician1)
16th Dec 2020, 3:54 PM
noteve
noteve - avatar
0
🎶💞Sravs💞🥀 Another thing, I didnt notice that I used yield to iterate then convert it to a list (which is longer😅), it can be shorten like this cause its just the same. def show_magicians(megicians): print(megicians) magicians = ["David","Doug Henning","Harry Anderson"] make_great(magicians) show_magicians(magicians)
17th Dec 2020, 12:20 AM
noteve
noteve - avatar
0
🎶💞Sravs💞🥀 Yes sure. It takes every element of the list "magicians" (which is ["David", "Doug Henning" ,"Harry Anderson"]) one by one which are strings , and replace that particular string with -"Great "+string- and store them inside the new list which is also named as "magicians". Hope you understand. :-) I have understood that but I did not understood the syntax of that line . Can you explain it?
17th Dec 2020, 4:53 AM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
0
Tq bhai
17th Dec 2020, 5:34 AM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
17th Dec 2020, 12:46 PM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
- 1
You seem to have been using wrong syntax. Lemme see how I can help
16th Dec 2020, 8:40 AM
Edmund Okoroka
Edmund Okoroka - avatar
- 1
Try this: def make_great(magicians): magicians=[j.replace(j,"Great "+j) for j in magicians] return magicians def show_magicians(magicians): for i in magicians: print(i) magicians = ["David","Doug Henning","Harry Anderson"] magicians=make_great(magicians) show_magicians(magicians) In this code I am not able to understand 2nd line Could you explain it
16th Dec 2020, 3:18 PM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
- 1
You keep the entire replace code within magicians [ ] that I did not understood
17th Dec 2020, 5:15 AM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
- 1
Why did you write that code in the list itself
17th Dec 2020, 5:17 AM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
- 1
def make_great(megicians): for i in range(len(megicians)): i.replace(i,"Great"+i) def show_magicians(megicians): print(magicians) magicians = ["David","Doug Henning","Harry Anderson"] make_great(magicians) show_magicians(magicians)
17th Dec 2020, 5:25 AM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar
- 1
TQ bhai
17th Dec 2020, 5:29 AM
🎶💞Sravs💞🥀
🎶💞Sravs💞🥀 - avatar