[Python] Errors in text-based game in python

Discussão em 'Python' iniciado por Stack, Agosto 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have it almost done but when invalid input is entered it throws an error instead of finishing the if statement and printing 'invalid input'. It also goes straight to winning the game when you choose whether you want to brew the potion or not. Other than that it is working.

    #define function for instructions: print main menu and commands
    def instructions():
    print('Magical Text Adventure Game')
    print('Collect 6 items and brew a potion to defeat the evil wizard'
    ' and save the people of Solis.')
    print('If you encounter the wizard before collecting all of the items and brewing the potion, '
    'the wizard will defeat you and the game will be over.')
    print('Move commands: go South, go North, go East, go West')
    print('Add to Inventory: get "item name"')
    print('----------------------------------------')

    #define function to show player their current status(location, inventory, item)
    def status(current_room, inventory, rooms):
    print('Inventory:{}'.format(inventory))
    print('You are in the {}.'.format(current_room))
    if 'item' in rooms[current_room]:
    print('Item: {}'.format(rooms[current_room]['item']))
    else:
    print('There is no item in this room.')

    #define main function(inventory, rooms)
    def main():
    rooms = {
    'Great Hall': {'South': 'Storage Room', 'North': 'Conservatory', 'East': 'Apothecary', 'West': 'Library'},
    'Storage Room': {'East': 'Dungeon', 'North': 'Great Hall', 'item': 'Bat Wing'},
    'Dungeon': {'West': 'Storage Room', 'item': 'Spider Silk'},
    'Apothecary': {'North': 'Kitchen', 'West': 'Great Hall', 'item': 'Gold Dust'},
    'Kitchen':{'South': 'Apothecary'},
    'Conservatory': {'South': 'Great Hall', 'East': 'Aviary', 'item': 'Lavender'},
    'Aviary': {'West': 'Conservatory', 'item': 'Phoenix Feather'},
    'Library': {'East': 'Great Hall', 'North': 'Bell Tower', 'item': 'Moonstone'},
    'Bell Tower': {'South': 'Library', 'item': 'Wizard'}

    }

    #player starts in Great Hall
    current_room = 'Great Hall'

    #player starts with empty inventory
    inventory = []

    #show player game instructions
    instructions()

    #gameplay loop
    while True:
    status(current_room, inventory, rooms)
    #get player input
    player_command = input('Enter your move:').title()
    player_command = player_command.split()
    #move player according to input
    if player_command[0] in rooms[current_room]:
    current_room = rooms[current_room][player_command[0]]
    elif player_command[1] in rooms[current_room]:
    current_room = rooms[current_room][player_command[1]]
    # add item to inventory
    elif player_command[0] in rooms[current_room]['item']:
    item = rooms[current_room]['item']
    inventory.append(item)
    del rooms[current_room]['item']
    elif player_command[1] in rooms[current_room]['item']:
    item = rooms[current_room]['item']
    inventory.append(item)
    del rooms[current_room]['item']
    else:
    print('Invalid Input')


    #brew potion
    if current_room == 'Kitchen':
    print('You are at the Kitchen.')
    player_input = input('Do you want to brew the potion?: yes or no')
    if player_input == 'yes' and len(inventory) == 6:
    inventory.clear()
    inventory.append('potion')
    print('You have brewed the potion! Now you can confront the wizard!')
    elif player_input == 'no':
    print('You have decided not to brew the potion. Come back later.')
    else:
    print('You do not have all of the items to brew the potion!')
    else:
    continue


    #if player runs into villain with potion (win) or without potion (lose)
    if current_room == 'Bell Tower' and inventory != 'potion':
    print('Uh Oh! You have encountered the wizard and do not have the potion to defeat him!')
    print('You Lose!')
    print('Thank you for playing!')
    break
    else:
    print('Congratulations!')
    print('You have defeated the wizard and saved the people of Solis!')
    print('Thank you for playing!')
    break

    main()


    I have tried mutiple things and can't make sense of why the invalid input check doesn't work.

    This is the error I keep receiving:

    Traceback (most recent call last):
    File "C:\Users\PycharmProjects\PythonProject2\TextBasedGame.py", line 99, in <module>
    main()
    ~~~~^^
    File "C:\Users\PycharmProjects\PythonProject2\TextBasedGame.py", line 56, in main
    elif player_command[1] in rooms[current_room]:
    ~~~~~~~~~~~~~~^^^
    IndexError: list index out of range

    Continue reading...

Compartilhe esta Página