ok So I am practiving a couple problems and have no idea how to start this one. I do not understand really well how to do nested loops, so we are given a list that creates a smiley face, and then this instructions.
smiley = [[" ","#"," ","#"," "],
[" ","#"," ","#"," "],
[" ","#"," ","#"," "],
[" "," "," "," "," "],
["#"," "," "," ","#"],
[" ","#"," ","#"," "],
[" ","#","#","#"," "],
[" "," "," "," "," "]]
Create a new local variable based on smiley defined above. More strictly, your definition of this new variable needs to explicitly use the variable smiley. Use nested for loops to change the characters stored in your newly created local variable. Do not add or remove any elements from any list. Only change/replace. You can, however, change and replace any of the characters with any other single characters. Return this local list of lists.
so how do I do nested loops for these list and change their items without changing everything else??
I only have this but I don't think it is even how I should start, please help me
def moodSwing():
face = smiley.copy()
for each in face[:1]:
for each in face[1:2]:
Try the following to switch between "#"
and " "
at the desired row number and character number:
def moodSwing(smiley, coords):
face = list(smiley)
for row, character in coords:
for r in range(len(face)):
for c in range(r):
if r == row+1 and c == character+1:
if face[r][c] == " ":
face[r][c] = "#"
else:
face[r][c] = " "
return face
Then do the following:
>>> frown = moodSwing(smiley, [(4, 0), (4, 4), (5, 1), (5, 2), (5, 3), (6, 1), (6, 2), (6, 3)])
for list_index, lst in enumerate(face):
for item_index, item in enumerate(lst):
if item == '#':
face[list_index][item_index] = ' '
else:
face[list_index][item_index] = '#'
The above code flips characters. item
is an element in the current list.
Use the indices to change elements.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.