0

I have looked at SO How can I reorder a list in python? for a solution. Yet, when I run my code, in 1 Python shell it works, and in the other it does not. Both are version 2.7. (EDIT: one version is 2.7, the other 3.2) The part of the code for v 2.7 where I receive an error (for v 3.2) is the following:

for r in range(c):  # c = 3 as I make 3 changes in the list
    p[list_a[r]], p[list_b[r]] = p[list_b[r]], p[list_a[r]]    # IndexError: list index out of range
    # p = [1, 2, 3, 4, 5] # p = starting list
    # list_a = [1,1,4]
    # list_b = [2,3,0]

    # ** Alternatively (brings same result): **
    #temp = p[list_a[r]]    # IndexError: list index out of range
    #p[list_a[r]] = p[list_b[r]]
    #p[list_b[r]] = temp

Why would I get different results (working or not working) for same version of Python? And how can I solve the IndexError? Thank you for your help.

EDIT: It seems that the online shell is in Python version 3.2. So may this be the reason why that version is giving me an error message?

Community
  • 1
  • 1
user1739581
  • 85
  • 2
  • 14
  • What's your workflow ? You type code directly in a python interpreter, import it from a file or launch python from shell ? – polku May 16 '16 at 15:46
  • 4
    The code runs fine for me. – kevmo314 May 16 '16 at 15:47
  • My workflow is the following: Working Windows Powershell: run a file and receive correct results in the shell. Not working: run code in online shell and this shell validates the result. – user1739581 May 16 '16 at 15:52
  • My guess is you actually have two versions of your code, that's why I asked this. For example if you import a file in python interpreter then modify it, your interpreter still uses the old version, I don't know the exact rules for this, but even re-importing it doesn't always help, you have to restart to really test the new version. But when you say "online shell that validates the result" do you mean like a website where a script test your code ? Because maybe it's simply that it finds an edge case you didn't tested. – polku May 16 '16 at 16:28

0 Answers0