Ich habe Code, das sofortiges Feedback - Sense erkennt, wie funktioniert, funktioniert aber nicht, wenn es in Pycharm ausgeführt wird. Es lautet wie folgt: < /p>
# First is the Room Dictionary, containing all of the Room IDs and the directions linked to them
rooms = {
'Main Entry': {'East': 'Main Floor'},
'Main Floor': {'North': 'North Hall', 'South': 'South Hall', 'East': 'Clothes Store', 'West': 'Main Entry'},
'South Hall': {'East': 'Food Court', 'North': 'Main Floor'},
'North Hall': {'East': 'Storage Room', 'South': 'Main Floor'},
'Clothes Store': {'North': 'Theater', 'West': 'Main Floor'},
'Theater': {'South': 'Clothes Store'}
}
#The current_room function tracks which "room" the user is in, and starts them in the Main Entry
current_room = 'Main Entry'
#The while loop plays until the user enters exit, to which it then ends the game.
while current_room != 'exit':
print(f"You are currently in the {current_room}")
command = input("Enter command: ")
#The commands are what moves the player around. If a direction is input that leads to another room, the current room
#is changed to match that direction. If the direction goes nowhere, the program will have the user input something else.
#Finally, if the input is neither a direction nor exit, it will output the error message
if command == 'exit':
current_room = 'exit'
elif command in ['North', 'South', 'East', 'West']:
direction = command.split(' ')[1]
if direction in rooms[current_room]:
current_room = rooms[current_room][direction]
else:
print("Can't go that way")
else:
print("No can do")
print("You have exited the game.")
< /code>
Die spezifische Fehlermeldung, die ich bekomme, lautet wie folgt: < /p>
Traceback (most recent call last):
File "C:\Users\patri\PycharmProjects\PythonProject\.venv\ModuleSixMilestone.py", line 26, in
direction = command.split(' ')[1]
~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
< /code>
Was verursacht diesen Fehler und wie kann ich ihn beheben? Gibt es verwandte Fehler, die ich ebenfalls herausfinden muss?
Wechsel zwischen den Zimmern V5 ⇐ Python
-
- Similar Topics
- Replies
- Views
- Last post