我是编码初学者,目前正在制作角色扮演游戏。当我运行代码时,它给了我上述错误。给我错误的部分是 print("You are on " + floors[currentRoom] + ". You find " + floorsFeature[currentRoom])
。我的猜测是 floors、floorsFeature 和 currentRooms 一定有问题。因为我是初学者,所以我不确定错误是什么意思。有人可以用简单的术语解释一下吗?
print("You are finally a Pokemon trainer! Today, you have gotten your very first Pokemon, a Bulbasaur!")
name = input("What will you name your Bulbasaur? ")
print("Unfortunately, " + name + " doesn't seem to obey or like you...")
print("You try to be friendly to " + name + ", but it just won't listen...")
print("As " + name + " was busy ignoring you, something seems to catch its attention and it runs off!")
print("You chase after " + name + ", but it's too fast! You see it running into an abandoned Pokeball Factory.")
print("You must explore the abandoned Pokeball Factory and find " + name + " before something happens to it!")
print()
print("You may input 'help' to display the commands.")
print()
gamePlay = True
floors = [['floor 1 room 1', 'floor 1 room 2', 'floor 1 room 3', 'floor 1 room 4'],['floor 2 room 1', 'floor 2 room 2', 'floor 2 room 3', 'floor 2 room 4', 'floor 2 room 5'],['floor 3 room 1,' 'floor 3 room 2', 'floor 3 room 3'],['floor 4 room 1', 'floor 4 room 2']]
floorsFeature = [['nothing here.', 'nothing here.', 'stairs going up.', 'a Squirtle.'],['stairs going up and a pokeball.', 'a Charmander.', 'a FIRE!!!', 'stairs going down.', 'a pokeball.'],['stairs going down.', 'a door covered in vines.', '2 pokeballs!'],['your Bulbasaur!!!', 'an Eevee with a key tied around its neck.']]
currentRoom = [0][1]
pokemonGot = []
count = 0
bagItems = []
countItems = 0
while gamePlay == True:
print("You are on " + floors[currentRoom] + ". You find " + floorsFeature[currentRoom])
move = input("What would you like to do? ")
while foo(move) == False:
move = input("There's a time and place for everything, but not now! What would you like to do? ")
if move.lower() == 'left':
if currentRoom > 0:
currentRoom = currentRoom - 1
print("Moved to " + floors[currentRoom] + ".")
else:
print("*Bumping noise* Looks like you can't go that way...")
elif move.lower() == 'right':
if currentRoom < len(floors) - 1:
currentRoom = currentRoom + 1
print("Moved to " + floors[currentRoom] + ".")
else:
print("*Bumping noise* Looks like you can't go that way...")
elif move.lower() == 'help':
print("Input 'right' to move right. Input 'left' to move left. Input 'pokemon' to see what Pokemon are on your team. Input 'bag' to see the items you are carrying. Input 'help' to see the commands again.")
elif move.lower() == 'pokemon':
if count == 0:
print("There are no Pokemon on your team.")
else:
print("The Pokemon on your team are: " + ", ".join(pokemonGot) + ".")
elif move.lower() == 'bag':
if countItems == 0:
print("There are no items in your bag.")
else:
print("The items in your bag are: " + ", ".join(bagItems) + ".")
print()
原文由 Adilene 发布,翻译遵循 CC BY-SA 4.0 许可协议
我无法使用提供的代码复制您的错误,因为它引发了另一个错误(列表索引超出范围)
我怀疑问题出在这里
这意味着您正在尝试将 currentRoom 的值设置为 [0] 的索引 1,它不存在。
[0] 这是一个包含一项的列表。如果你对此感到困惑,启动 python3,然后试试这个
根据您的示例,您正在为楼层列表使用嵌套列表。
floor[0] = 1 楼,floor [0] = 2 楼,等等。
在 1 楼,您有另一个列表,其中每个索引决定了您当前的房间。
使用两个整数来保存当前位置怎么样?