【问题标题】:Index Error: list index out of ranged (python 3.2)索引错误:列表索引超出范围(python 3.2)
【发布时间】:2017-06-23 08:48:44
【问题描述】:

我和我的朋友们正在学校做一个项目,我们理解错误出现的原因,但我们看不出我们在哪里犯了这个错误。我们确信我们算对了,我们知道在索引中它从 0 开始,但它仍然是错误的。我们的任务是创建一个让玩家在 7x7 网格中移动的游戏,但我们必须从存储在同一文件中的代码之外插入一些游戏消息。 P.S 我们可以寻求帮助。 这是代码请帮忙!:

import random


counter1 = 0

 counter2 = 0


print("***********************************BOARD GAME**********************************")

 print(" ")

 print("***********************************GAME RULES**********************************")

 print(" ")

 print("----------------------------Game is for 2 players only-------------------------")

print(" ")

 print(">The game board is a 10x5 board going up to 49")

 print(" ")

 print("40 41 42 43 44 45 46 47 48 49")

 print("39 38 37 36 35 34 33 32 31 30")

 print("20 21 22 23 24 25 26 27 28 29")

 print("19 18 17 16 15 14 13 12 11 10")

 print("0  1  2  3  4  5  6  7  8  9 ")

 print(" ")

 print(">The objective is to be the first player to reach space 49")

 print(" ")

 print(">There are 2 die, if you roll the same number twice, you will go back the number of spaces you rolled")

 print(" ")

 print(">If you land on spaces 27, 31 and 47, you will go back to space 24")

 print(" ")

 print(">Press ENTER to play")

 input()


print("**********************************START GAME***********************************")

input()


print("Starting positions for both players = 0")

print(" ")


newfile=open("Game Messages.txt","r")

 print(newfile.readlines()[0])

 dice1 = random.randint(1,6)

 print("dice 1 =",dice1)

 dice2 = random.randint(1,6)

 print("dice 2 =",dice2)

 dicetotal = dice1 + dice2

 print("dice total =",dicetotal)

 if dice1 == dice2:

     counter1 = counter1 - dicetotal

     print(newfile.readlines()[1])    #This is one of the lines that is coming up as an error


 else:

     counter1 = counter1 + dicetotal

 print("P1 space =",counter1)

 if counter1 == 47:

     counter1 = 24

     print(newfile.readlines()[2])

 if counter1 == 27:

     counter1 = 24

     print(newfile.readlines()[3])

 if counter1 == 31:

     counter1 = 24

     print(newfile.readlines()[4])

 if counter1 >= 49:

     print(newfile.readlines()[5])

     print("end game end game end game end game end game end game end game end game end game")

     print("Press ENTER to exit the game")

     exit()

     input()

 input()

 newfile.close


newfile = open("Game Messages.txt.","r")    

 print(newfile.readlines()[6])

 dice1 = random.randint(1,6)

 print("dice 1 =",dice1)

 dice2 = random.randint(1,6)

 print("dice 2 =",dice2)

 dicetotal = dice1 + dice2

 print("dice total =",dicetotal)

 if dice1 == dice2:

     counter2 = counter2 - dicetotal

     print(newfile.readlines()[7])     #This is one of the lines that is coming up as an error

 else:

     counter2 = counter2 + dicetotal

 print("P2 space =",counter2)

 if counter2 == 47:

     counter2 = 24

     print(newfile.readlines()[8])

 if counter2 == 27:

     counter2 = 24

     print(newfile.readlines()[9])

 if counter2 == 31:

     counter2 = 24

     print(newfile.readlines()[10])

 if counter2 >= 49:

     print(newfile.readlines()[11])

     print("end game end game end game end game end game end game end game end game end game")

     print("Press ENTER to exit the game")

     exit()

     input()

 input()

 newfile.close

【问题讨论】:

    标签: python-3.2 index-error


    【解决方案1】:

    实际上问题在于从 Game Messages.txt 中获取文件内容。 因此,首先需要将整个文件内容转换为列表。

    请先在您的程序中执行此程序

    with open('Game Messages.txt', 'r') as infile:
        data = infile.read()  # Read the contents of the file into memory.
        
        # Return a list of the lines, breaking at line boundaries.
        my_list = data.splitlines()
        print(my_list)  # It will give whole file contents as an array
        # E.g.: ['game message1', 'game message2', 'game message3', 'game message4', 'game message5']
        print(my_list[3])  # It will display game message 4
    

    所以在你的程序中而不是

    if dice1 == dice2:
        counter2 -= dicetotal
        
        print(newfile.readlines()[7])  # This is one of the lines that is coming up as an error    
    else:
        counter2 += dicetotal
    

    会的

    if dice1 == dice2:
        counter2 -= dicetotal
        
        print(my_list[3])     #You need to give exact line number, in case 4th means it #will be my_list[3] as array index starts with 0 only
    else:
        counter2 += dicetotal
        
    

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多