4

I cannot get this code to work. I get an error for line 4 but I don't understand how it could possibly be out of range and why I cannot get this to swap the N to be upside down.

def verticalSwap(n):
    for i in range(len(n)):
        row1 = n[i]
        row2 = n[(len(n))-i]
        temp = row2
        row2 = row1
        row1 = temp


n = ["@         @", "@ @       @", "@   @     @", "@     @   @", "@       @ @", "@         @"]
for i in range(len(n)):
    print(n[i])
verticalSwap(n)
print("")
for i in range(len(n)):
    print(n[i])

The output is supposed to look like

@         @
@       @ @
@     @   @
@   @     @
@ @       @
@         @
Isaiah115
  • 41
  • 3
  • @ggorlen Hi, thanks! The output is supposed to be a completely upside down version of the original N. – Isaiah115 May 04 '20 at 00:00
  • I'm not sure what that means, exactly (you can edit the post to show rather than tell--comments are not suitable for code), but why not just `n[::-1]`? – ggorlen May 04 '20 at 00:01
  • @ggorlen what does the :: do? Btw I edited the post – Isaiah115 May 04 '20 at 00:02
  • Thanks for the edit! `[::-1]` reverses the list. See [this](https://stackoverflow.com/questions/3940128/how-can-i-reverse-a-list-in-python): `for row in n[::-1]: print(row)`. – ggorlen May 04 '20 at 00:03
  • @ggorlen oh, that would work a lot better. Let me try to fix the code. – Isaiah115 May 04 '20 at 00:04

2 Answers2

1

Your array indices range from 0 to len(n)-1. Thus, you should change line 4 to row2 = n[(len(n)) - i - 1].

Two additional comments:

  1. I would suggest changing the variable name n to something more meaningful.
  2. Your function currently does not do any actual swapping but then your code is probably just work-in-progress.
Srikant
  • 294
  • 1
  • 2
  • 3
  • Thanks, that makes sense. My code actually isnt supposed to be a work in progress haha, why doesn't it swap? – Isaiah115 May 04 '20 at 00:07
  • In your loop, you create a bunch of local variables that have nothing to do with the list. If `n[i]` isn't on the left hand side of an assignment, e.g. `n[i] = something`, nothing will be assigned to the list element. Integers are simply copied, not referenced. – ggorlen May 04 '20 at 00:10
  • @ggorlen ohh. Lol I should've realized that. – Isaiah115 May 04 '20 at 00:11
  • These things are non-obvious at first, but printing the data in the loop is a good way to try debugging it. You'll see that the `n` list isn't changing after an assignment and might investigate further. – ggorlen May 04 '20 at 00:13
0

len(n) - i is out of bounds if i is 0. Also, your function makes new variables instead of changing the original list. You can also make swap the pairs directly without needing to make a new variable.

Iterating up to len(n) would swap everything twice, essentially doing nothing. You want to iterate up to len(n) // 2. Here's an updated version:

def verticalSwap(n):
    for i in range(len(n) // 2):
        n[i], n[len(n) - i - 1] = n[len(n) -i - 1], n[i]
Daniel Giger
  • 2,023
  • 21
  • 20