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
23 odpowiedzi
+ 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)
+ 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.
+ 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. :-)
+ 3
๐ถ๐Sravs๐๐ฅ
Is this what you do not understand?
String.replace(old substring to be replaced, new substring which would replace the old substring)
+ 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]
+ 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.
+ 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.
+ 2
Analyze this code, hope it will help you :)
https://code.sololearn.com/cTQOH0eJclS5/?ref=app
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
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)
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)
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?
0
Tq bhai
- 1
You seem to have been using wrong syntax.
Lemme see how I can help
- 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
- 1
You keep the entire replace code within magicians [ ] that I did not understood
- 1
Why did you write that code in the list itself
- 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)
- 1
TQ bhai