'Code returns TypeError and I'm not sure why

The code below returns 'TypeError: 'int' object is not subscriptable' when move_pas is called. The thnig is, is that the code affecting this is also in move_ag, but when I call that there is no error and I cant figure out why that is. I'm running this in python3.9.2 in vscode if that matters. Thanks in advance. (sorry for bad english)

import random

char_list = []
stance_list = []
spot_list = []
status_list = []
amount_list = []


def remake_name_list():
    global name_list
    name_list = ["Josh", "Jeff", "Lisa", "Addie", "Jack", "John", "Zac", "Evan", "Brayden", "Seb", "Sab", "Nick", "Dom", "Rex", "James", "Robert",
                 "John", "Michael", "William", "David", "Richard", "Joseph", "Thomas", "Charles", "Christopher", "Daniel", "Matthew", "Anthoney", "Mark"]


remake_name_list()


def gen(amount):
    for i in range(0, amount):
        rand = random.choice(name_list)
        char_list.append(rand)
        name_list.remove(rand)
        rand = random.randint(0, 8)
        if rand == 8:
            stance_list.append("aggressive")
        elif rand != 8:
            stance_list.append("passive")
        rand = random.randint(0, amount*2)
        spot_list.append(rand)
        amount_list.append(2)
        amount_list.append(2)
        rand = random.randint(1, 2)
        if rand == 1:
            amount_list.append(2)
        elif rand == 2:
            pass
            for i in range(0, max(spot_list)):
                if spot_list.count(i) >= 3:
                    amount_list.clear()
                    gen(amount)


def move_ag(amount_list, stance_list):
    if 'aggressive' in stance_list:
        for i in range(0, len(char_list)):
            if stance_list[i] == "aggressive":
                am = spot_list.count(spot_list[i])
                if am == 1:
                    stance_list[i] = 2
                    amount_list[spot_list[i]] -= 2
                elif am == 2:
                    stance_list[i] = 1.5
                    amount_list[spot_list[i]] -= 1.5


def move_pas(amount_list, stance_list):
    if 'passive' in stance_list:
        for i in range(0, len(char_list)):
            if stance_list[i] == "passive":
                am = spot_list.count(spot_list[i])
                if am == 1:
                    stance_list[i] = 2
                    amount_list[spot_list[i]] -= 2
                elif am == 2:
                    if amount_list[i] == 1:
                        stance_list = 1
                        amount_list[spot_list[i]] -= 1
                    if amount_list[i] == 2:
                        stance_list = 1
                        amount_list[spot_list[i]] -= 1
                    if amount_list[i] == 0.5:
                        stance_list = 0.5
                        amount_list[spot_list[i]] -= 0.5


amount = 10
gen(amount)
move_ag(amount_list, stance_list)
move_pas(amount_list, stance_list)
print(stance_list)
print(amount_list)


Solution 1:[1]

The way that you have it:

def move_pas(amount_list, stance_list):
    if 'passive' in stance_list:
        for i in range(0, len(char_list)):
            if stance_list[i] == "passive":
                am = spot_list.count(spot_list[i])
                if am == 1:
                    stance_list[i] = 2
                    amount_list[spot_list[i]] -= 2
                elif am == 2:
                    if amount_list[i] == 1:
                        stance_list = 1 #------------------------- here
                        amount_list[spot_list[i]] -= 1
                    if amount_list[i] == 2:
                        stance_list = 1 #------------------------- here
                        amount_list[spot_list[i]] -= 1
                    if amount_list[i] == 0.5:
                        stance_list = 0.5 #----------------------- here
                        amount_list[spot_list[i]] -= 0.5

If any of the if statements is met the stance_list is changed to an integer (1 or 0.5). So the next time the loop goes on it will meet this statement if stance_list[i] == "passive" and will tell you that a integer cannot be subscripted (Which is to be expected).

I assume what you want to do is to change the i-th element of stance_list to that number. Like this:

def move_pas(amount_list, stance_list):
    if 'passive' in stance_list:
        for i in range(0, len(char_list)):
            print(stance_list)
            if stance_list[i] == "passive":
                am = spot_list.count(spot_list[i])
                if am == 1:
                    stance_list[i] = 2
                    amount_list[spot_list[i]] -= 2
                elif am == 2:
                    if amount_list[i] == 1:
                        stance_list[i] = 1 #------------------------- here
                        amount_list[spot_list[i]] -= 1
                    if amount_list[i] == 2:
                        stance_list[i] = 1 #------------------------- here
                        amount_list[spot_list[i]] -= 1
                    if amount_list[i] == 0.5:
                        stance_list[i] = 0.5 #----------------------- here
                        amount_list[spot_list[i]] -= 0.5

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 D.Manasreh