0

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]:
1
  • Don't reassign each, else if you try to access the first assignment it will redirect you to the second. Commented Sep 3, 2015 at 2:30

2 Answers 2

1

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)])
Sign up to request clarification or add additional context in comments.

1 Comment

thank you I understand this. What if I do not want the function to take in any variables and lets say I run it and want my 'smiley' face to be sad and look like this: [" ","#"," ","#"," "] [" ","#"," ","#"," "] [" ","#"," ","#"," "] [" "," "," "," "," "] [" "," "," "," "," "] [" ","=","=","="," "] [" "," "," "," "," "] [" "," "," "," "," "] ? so when I go to the shell and type : >>> aVariable = moodSwing() >>> for line in aVariable: print(line)
1
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.

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.