-1

image of code if not understandable

The image shows the code I have written, I want to swap item positions but I am getting list indices must be integer or slice not string. I have tried many different ways but still dont seem to get the hang of it.

choice = ""
lists = []
while True:

choice = input("Try out inv commands: ")
if choice == 'inv':
    print('Backpack has ' + str(len(lists)) + ' iteams: ' + str(lists))
elif 'inv pick' in choice:
    iteams1 = choice.split('inv pick ')
    iteams2 = iteams1[1].split(' ')
    try:
        if iteams2[1] != '':
            add = lists.insert(int(iteams2[1]), iteams2[0])
            print(str(iteams2), ' has been added')
    except IndexError:
        lists.extend(iteams2)
elif 'inv drop' in choice:
    iteams1 = choice.split('inv drop ')
    iteams2 = iteams1[-1].split(' ')
    if iteams2[-1] != '':
        removed = lists.remove(iteams2[0])
        print(str(iteams2), ' has been dropped')
elif 'inv swap' in choice:
    iteams1 = choice.split('inv swap')
    iteams2 = iteams1[1].split(' ')
    try:
        if iteams2[1] != '':
            pos1, pos2 = iteams2[2], iteams2[1]
            lists[pos1], lists[pos2] = lists[pos2], lists[pos1]
            print('swapped ')
    except IndexError:
        enter code herelists.extend(iteams2)
Naveed
  • 17
  • 1
  • 3
  • 2
    please paste code – Anmol Parida Oct 06 '20 at 15:37
  • 3
    Welcome to SO. Please don't post code as images, instead paste it in the question and format the code. – Wasif Oct 06 '20 at 15:37
  • try ```pos1, pos2 = int(iteams2[2]), int(iteams2[1])``` – Anmol Parida Oct 06 '20 at 15:40
  • 2
    Please include your code as a [formatted code block](https://stackoverflow.com/help/formatting) instead of an image. [Why do we hate screenshots so much?](https://meta.stackoverflow.com/questions/303812/) | [How to debug small programs.](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) | [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/843953) – Pranav Hosangadi Oct 06 '20 at 15:40
  • @AnmolParida I just posted – Naveed Oct 06 '20 at 15:44
  • @WasifHasan Hello Wasif thank you, I just posted the code – Naveed Oct 06 '20 at 15:44

4 Answers4

0

Instead how about reversing the list?

lists.reverse()

If you use reversed(lists), then it will print the list reversed.

Wasif
  • 14,755
  • 3
  • 14
  • 34
0

Well, you see, on line 30 you assigned

pos1, pos2 = iteams[2], iteams[1]

That means that pos1 and pos2 are string variables. On line 31, you tried to use lists[pos2], but that means lists["something"] instead of lists[0]. Always bear in mind what you value and data type assign to each variable.

jinchuika
  • 333
  • 1
  • 8
0

As others have suggested, please follow the guideline to ask questions in stackoverflow to acommodate others in answering :)

To answer the question, the error says that it is caused by using a string on list indexes. Indeed, your variables are strings and you tried to use it in list indexes. e.g. x[0] to get element of a list on 0th position, but not x['0']

Instead, you can try using

  pos1, pos2 = int(iteams[2]), int(iteams[1]) 

but I'd suggest rewriting the code to avoid casting types and future confusions

dswij
  • 148
  • 6
0

As I see, you are giving strings as indices like the error mentions.

When you split the choice variable, it returns list of strings. You need to convert those strings to integers or slices if you would like to access elements in those indices.

pos1, pos2 = int(iteams2[2]),int(iteams2[-1])

This works in your case I guess.

Don't forget to paste your code to your question next time, screenshot would be hard to read for people :)

E. Dem
  • 67
  • 1
  • 7
  • @Dem i tried the method you suggested but i get this following error: pos1, pos2 = int(iteams2[2]), int(iteams2[-1]) ValueError: invalid literal for int() with base 10: 'b' – Naveed Oct 06 '20 at 16:09
  • @Naveed do you have numbers in `iteams2` list or are you trying to give letters as indices ? – E. Dem Oct 07 '20 at 07:49
  • @Dem I am giving letters as indices, e.g. inv swap a b. With help of swap i want change the elements position based on the inputted values by the user not by index. – Naveed Oct 07 '20 at 08:11
  • In a list you can't give letters as indices it should be integers. Instead of list maybe you can use dictionary and write it like `iteams2 = { 'a': 1, 'b': 2, 'c': 3 }` . In this way you can return integers from `iteams2` with `iteams2['a']` . – E. Dem Oct 07 '20 at 08:27