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

huangapple go评论88阅读模式
英文:

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

问题

这是你的代码部分的翻译:

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 = []  # 战斗者列表
attackers_list = []  # 攻击者列表
dummy_value = 0
fighter_num = random.randint(6, 10)  # 随机生成6到10之间的战斗者数量
attackers_num = random.randint(1, 6)  # 随机生成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:  # 此while循环用于确保for循环在其最后一次迭代时
        if dummy_value != fighter_num + 1:
            dummy_value += 1
            continue
        else:
            for i in range(attackers_num):
                rand_attacker = random.choice(fighters_list)  # 随机选择一个攻击者
                attackers_list.append(rand_attacker)
                fighters_list.remove(rand_attacker)  # 从战斗者列表中移除已选择的攻击者
            break

print(fighters_list)  # 打印剩余的战斗者列表
print(attackers_list)  # 打印攻击者列表

这是控制台输出的翻译:

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

请注意,这个错误是因为列表索引越界引起的,可能是因为在某个时刻战斗者列表为空了,但攻击者列表还在尝试选择攻击者。你可以添加一些条件来确保不会出现这种情况,比如检查战斗者列表是否为空后再选择攻击者。

英文:

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&quot;fighter{i}&quot;, 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 &quot;C:\Users\azaz9\PycharmProjects\oyrenme\yoxlama.py&quot;, line 38, in &lt;module&gt;
    rand_attacker = random.choice(fighters_list)
  File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\random.py&quot;, line 378, in choice
    return seq[self._randbelow(len(seq))]
IndexError: list index out of range

答案1

得分: 1

你只添加了一个战斗者,然后尝试获取攻击者。
这样,在第一次迭代后,列表就为空了,随后对.choice()的调用就会失败。

你应该添加所有的战斗者,然后选择攻击者:

英文:

You are appending only one fither, and then trying to get the attackers.
In this way after the first iteration the list is empty and the subsequent call to .choice() fails.

You should append all the fighters, than choose the attackers:

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&quot;fighter{i}&quot;, 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):
            print(len(fighters_list))
            rand_attacker = random.choice(fighters_list)
            atackers_list.append(rand_attacker)
            fighters_list.remove(rand_attacker)
        break

print(fighters_list)
print(atackers_list)

答案2

得分: 1

  • 你的 while 循环是无用的,只是以下代码:

    dummy_value = fighter_num + 1
    
    for i in range(atackers_num):
        rand_attacker = random.choice(fighters_list)
        atackers_list.append(rand_attacker)
        fighters_list.remove(rand_attacker)
    
  • 第一个 for 循环初始化了一些东西,"while" 循环(或者只是主要的代码)应该在它之外/之后。

结果如下:

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

dummy_value = fighter_num + 1

for i in range(atackers_num):
    rand_attacker = random.choice(fighters_list)
    atackers_list.append(rand_attacker)
    fighters_list.remove(rand_attacker)
英文:
  • your while loop is useless, that is just the following code

    dummy_value = fighter_num + 1
    
    for i in range(atackers_num):
        rand_attacker = random.choice(fighters_list)
        atackers_list.append(rand_attacker)
        fighters_list.remove(rand_attacker)
    
  • the first for loop initializes the stuffs, the "while" loop (or just the main code) should be outside/after it

Resulting in

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&quot;fighter{i}&quot;, fighter_health, fighter_power, fighter_stamina)
    fighters_list.append(fighter.name)
    dummy_value += 1

dummy_value = fighter_num + 1

for i in range(atackers_num):
    rand_attacker = random.choice(fighters_list)
    atackers_list.append(rand_attacker)
    fighters_list.remove(rand_attacker)

huangapple
  • 本文由 发表于 2023年7月7日 01:45:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定