错误:在游戏代码中列出索引超出范围

时间:2015-06-12 14:58:02

标签: python

我遇到列表索引错误,代码可能不是最清晰的。但是我对Python中的编码知之甚少,但我知道编写游戏的代码很多。

该函数的代码:

#Checks if the move counter is more than 3, if it is, the player gets to decide what he/she wants to do
    if Moves != 0:
        Do = input('What do you do? Help to see all commands ')

        #Tests for if the player wants a magic spell
        if Do == 'Magic':
            if Moves == 3:
                ListToDo = MagicDmg(ListToDo, Moves)
                if ListToDo[0] == 'Ice' or 'Fire' or 'Bolt':
                    Mana = CheckMagicCost1(ListToDo, Mana, Moves)
                else:
                    Moves = Moves - 1
            elif Moves == 2:
                ListToDo = MagicDmg(ListToDo, Moves)
                Mana = CheckMagicCost2(ListToDo, Mana, Moves)
                Moves = Moves - 1
            else:
                ListToDo = MagicDmg(ListToDo, Moves)
                Mana = CheckMagicCost3(ListToDo, Mana, Moves)
                Moves = Moves - 1

#This function does not handle damage
def MagicDmg(ListToDo, Moves):
    print('Which magic? Ice, Fire or Bolt')
    MagicDo = input()
    if MagicDo != 'Ice' or 'Fire' or 'Bolt':
        if MagicDo == 'Ice':
            ListToDo.append('Ice')
        elif MagicDo == 'Fire':
            ListToDo.append('Fire')
        elif MagicDo == 'Bolt':
            ListToDo.append('Bolt')
    else:
        LIstToDo.remove(MagicDo)
        print('That is not a valid spell')
        Moves = Moves + 1
    return(ListToDo)

#Subtracts the mana cost of spells from mana
def CheckMagicCost1(ListToDo, Mana, Moves):
    if ListToDo[0] and Mana >= 10 == 'Ice':
        Mana = Mana - 10
    elif ListToDo[0] and Mana >= 15 == 'Fire':
        Mana = Mana - 15
    elif ListToDo[0] and Mana >= 25 == 'Bolt':
        Mana = Mana - 25
    else:
        print('You do not have enough Mana')
        Moves = Moves + 1
    return(Mana)

def CheckMagicCost2(ListToDo, Mana, Moves):
    if ListToDo[1] and Mana >= 10 == 'Ice':
        Mana = Mana - 10
    elif ListToDo[1] and Mana >= 15 == 'Fire':
        Mana = Mana - 15
    elif ListToDo[1] and Mana >= 25 == 'Bolt':
        Mana = Mana - 25
    else:
        print('You do not have enough Mana')
        Moves = Moves + 1
    return(Mana)

def CheckMagicCost3(ListToDo, Mana, Moves):
    if ListToDo[2] and Mana > 9 == 'Ice':
        Mana = Mana - 10
    elif ListToDo[2] and Mana > 14 == 'Fire':
        Mana = Mana - 15
    elif ListToDo[2] and Mana > 24 == 'Bolt':
        Mana = Mana - 25
    else:
        print('You do not have enough Mana')
        Moves = Moves + 1
    return(Mana)

结果:

A Azure Drake appeared! With 500 HP!

HP = 250 Mana = 250
Moves left: 3 

Boss's HP: 500 

What do you do? Help to see all commands Magic
Which magic? Ice, Fire or Bolt
i (False spell)
Traceback (most recent call last):
  File "D:\Python\Battle Test V2.py", line 186, in <module>
    if ListToDo[0] != 'Ice' or 'Fire' or 'Bolt':
IndexError: list index out of range

2 个答案:

答案 0 :(得分:1)

我无法立即判断这是否是您唯一的问题,但这一行绝对不会 工作。

if MagicDo != 'Ice' or 'Fire' or 'Bolt':

真正检查的是:

if (MagicDo != 'Ice') or ('Fire' != None) or ('Bolt' != None):

可能的解决办法是改为:

if MagicDo in(['Ice','Fire','Bolt']):

这将检查MagicDo是否是您想要的三种法术之一。同样,我不确定这是否是您唯一的问题,但这绝对是一个问题。

编辑:我也注意到CheckMagicCost功能中的问题。你的条件都不会做你想做的事。例如,行

if ListToDo[0] and Mana >= 10 == 'Ice':

确实在检查以下内容

if (ListToDo[0] != None) and ((Mana >= 10) == 'Ice'):

我猜你想要的更像是以下内容:

if ListToDo[0] == 'Ice' and Mana >= 10:

此更改应传播到CheckMagicCost1CheckMagicCost2CheckMagicCost3函数中的其他条件。

答案 1 :(得分:0)

没有错误代码就没那么多了,但我的直觉是你会在你的函数中遇到这些语句的问题:

if ListToDo[1]

可能更好地包装在try块中并捕获IndexError?只是一个想法

try:
    #code
except IndexError:
    #contingency plan