jeudi 6 juillet 2023

Im getting a Index Error when i tried to make a dummy console game with basic oop

Now i will give you more details, the game has max 10 min 6 players,they should randomly choose another player and attack them. So here in for loop i am tring to randomly choose who is gonna atack and who is going to get hurt, but it gives error

import random

class Fighter:
    def __init__(self, name, health, power, stamina):
        self.name = name
        self.health = health
        self.power = power
        self.stamina = stamina
        self.fighter_stats = [self.name, self.health, self.power, self.stamina]

    @classmethod
    def Get_Hurt(cls):
        pass

    @classmethod
    def Attack(cls):
        pass

fighters_list = []
atackers_list = []
dummy_value = 0
fighter_num = random.randint(6, 10)
atackers_num = random.randint(1, 6)

for i in range(1, fighter_num + 1):
    fighter_health = random.randint(55, 100)
    fighter_power = random.randint(20, 35)
    fighter_stamina = random.randint(3, 7)
    fighter = Fighter(f"fighter{i}", fighter_health, fighter_power, fighter_stamina)
    fighters_list.append(fighter.name)
    dummy_value += 1
    while True:    #this while loop is for making sure if the for loop is in its last loop  
        if dummy_value != fighter_num + 1:
            dummy_value += 1
            continue
        else:
            for i in range(atackers_num):
                rand_attacker = random.choice(fighters_list)
                atackers_list.append(rand_attacker)
                fighters_list.remove(rand_attacker)
            break

print(fighters_list)
print(atackers_list)

And that is the console output:

Traceback (most recent call last):
  File "C:\Users\azaz9\PycharmProjects\oyrenme\yoxlama.py", line 38, in <module>
    rand_attacker = random.choice(fighters_list)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\random.py", line 378, in choice
    return seq[self._randbelow(len(seq))]
IndexError: list index out of range



Aucun commentaire:

Enregistrer un commentaire