[Python] Program Functional but Output is Incorrect

Discussão em 'Python' iniciado por Stack, Abril 18, 2025.

  1. Stack

    Stack Membro Participativo

    I am taking an introductory class in Python right now. My assignment is to create a text based adventure that requires players to move through different rooms and pick up all items before reaching the final room. My code is fully functional, however, I am having trouble with one conditional. My program is supposed to print 'Invalid Input' and reprompt the player for an input if they type anything other than the intended commands ('Exit', 'Get item', 'Go *directional'). However, I am noticing that I am able to misspell commands and the game continues to progress.

    #start player in the Entrance of Darkness
    current_room = 'Entrance of Darkness'
    #start player with empty inventory
    inventory = []

    #show instructions function
    def show_instructions():
    #welcome player to the game and show instructions
    print('Welcome to the game!')
    print('Hades has stolen 6 precious artifacts causing chaos in the Overworld. Find all 6 items before facing Cerberus to escape the Underworld and become a hero!')
    print('Move commands: Go North, Go South, Go East, Go West')
    print("To add to Inventory: Get item")
    print('To exit the game type: Exit')
    return '--------------------'

    #player status function
    def player_status():
    #show player location
    global current_room
    print('You are in the {}'.format(current_room))
    #show player inventory
    global inventory
    print('Inventory:{}'.format(inventory))
    return('--------------------')

    def main():
    #dictionary of rooms, possible moves, and items
    rooms = {
    'Entrance of Darkness': {'North': 'Thunderous Throne Room', 'East': 'The Grove of Colchis'},
    'The Grove of Colchis': {'North': 'The Chamber of Chaos', 'West': 'Entrance of Darkness', 'Item': 'Chrysomallos\'s Golden Fleece'},
    'The Chamber of Chaos': {'South': 'The Grove of Colchis', 'West': 'Thunderous Throne Room', 'Item': 'Pandora\'s Box'},
    'Thunderous Throne Room': {'North': 'Underwater Kingdom', 'South': 'Entrance of Darkness', 'East': 'The Chamber of Chaos', 'West': 'Archery Isle', 'Item': 'Zeus\'s Lightning Bolt'},
    'Underwater Kingdom': {'North': 'Hero Hall', 'South': 'Thunderous Throne Room', 'Item': 'Poseidon\'s Trident'},
    'Hero Hall': {'South': 'Underwater Kingdom', 'Item': 'Persus\'s Sword'},
    'Archery Isle': {'North': 'Cerberus\'s Lair', 'East': 'Thunderous Throne Room', 'Item': 'Apollo\'s Bow'},
    'Cerberus\'s Lair': {'South': 'Archery Isle'}

    }

    #list of possible moves
    directions = ['North', 'South', 'East', 'West']

    print(show_instructions())

    #start loop for gameplay when player input is not exit
    while True:
    #call needed variables into function
    global current_room
    global inventory

    #display player status function
    print(player_status())

    #display item in current room
    if 'Item' in rooms[current_room].keys():
    room_item = rooms[current_room]['Item']
    if room_item not in inventory:
    print('You see {}'.format(room_item))

    #get player command
    player_input = input('Enter your move:').strip().title()

    #command for picking up item
    if player_input.split()[0] == 'Get':
    inventory.append(room_item)
    print('You picked up {}'.format(room_item))
    print('--------------------')

    #condition for breaking the loop
    elif player_input == 'Exit':
    print('Thanks for playing the game. Goodbye.')
    exit()

    #account for invalid inputs
    else:
    print('Invalid input')

    #define the directional as the first element of the player's input
    user_direction = player_input.split()[1]

    #validate directions
    if user_direction in directions:
    if user_direction in rooms[current_room]:
    current_room = rooms[current_room][user_direction]
    #condition for if direction is not an option
    else:
    print('You can\'t go that way!')
    print('--------------------')

    #lose condition for the game
    if current_room == 'Cerberus\'s Lair' and len(inventory) < 6:
    print('You have run into Cerberus without collecting all your items! Game over.')
    exit()

    #win condition for the game
    if current_room == 'Cerberus\'s Lair' and len(inventory) == 6:
    print('You have collected all the stolen items and defeated Cerberus. Congratulations!')
    exit()

    while True:
    main()


    I think my issue is that my code is detecting if the player enters a valid directional such as North or South, meaning they can type any word other than 'Go' before this and still have the intended outcome. Same with the 'Get item' function, as long as 'Get' is typed correctly the program executes my conditional. I tried updating the input requirement to be 'Get item', however I get an invalid input error and the item is not added to my inventory. I am lost on what to do next. This is how I restructured it to try to solve my problem, but this isn't working either.

    #start player in the Entrance of Darkness
    current_room = 'Entrance of Darkness'
    #start player with empty inventory
    inventory = []

    #show instructions function
    def show_instructions():
    #welcome player to the game and show instructions
    print('Welcome to the game!')
    print('Hades has stolen 6 precious artifacts causing chaos in the Overworld. Find all 6 items before facing Cerberus to escape the Underworld and become a hero!')
    print('Move commands: Go North, Go South, Go East, Go West')
    print("To add to Inventory: Get item")
    print('To exit the game type: Exit')
    return '--------------------'

    #player status function
    def player_status():
    #show player location
    global current_room
    print('You are in the {}'.format(current_room))
    #show player inventory
    global inventory
    print('Inventory:{}'.format(inventory))
    return('--------------------')

    def main():
    #dictionary of rooms, possible moves, and items
    rooms = {
    'Entrance of Darkness': {'North': 'Thunderous Throne Room', 'East': 'The Grove of Colchis'},
    'The Grove of Colchis': {'North': 'The Chamber of Chaos', 'West': 'Entrance of Darkness', 'Item': 'Chrysomallos\'s Golden Fleece'},
    'The Chamber of Chaos': {'South': 'The Grove of Colchis', 'West': 'Thunderous Throne Room', 'Item': 'Pandora\'s Box'},
    'Thunderous Throne Room': {'North': 'Underwater Kingdom', 'South': 'Entrance of Darkness', 'East': 'The Chamber of Chaos', 'West': 'Archery Isle', 'Item': 'Zeus\'s Lightning Bolt'},
    'Underwater Kingdom': {'North': 'Hero Hall', 'South': 'Thunderous Throne Room', 'Item': 'Poseidon\'s Trident'},
    'Hero Hall': {'South': 'Underwater Kingdom', 'Item': 'Persus\'s Sword'},
    'Archery Isle': {'North': 'Cerberus\'s Lair', 'East': 'Thunderous Throne Room', 'Item': 'Apollo\'s Bow'},
    'Cerberus\'s Lair': {'South': 'Archery Isle'}

    }

    #list of possible moves
    directions = ['North', 'South', 'East', 'West']

    print(show_instructions())

    #start loop for gameplay when player input is not exit
    while True:
    #call needed variables into function
    global current_room
    global inventory

    #display player status function
    print(player_status())

    #display item in current room
    if 'Item' in rooms[current_room].keys():
    room_item = rooms[current_room]['Item']
    if room_item not in inventory:
    print('You see {}'.format(room_item))

    #get player command
    player_input = input('Enter your move:').strip().title()

    #condition for breaking the loop
    if player_input == 'Exit':
    print('Thanks for playing the game. Goodbye.')
    exit()

    #command for picking up an item
    elif player_input == ('Get item'):
    inventory.append(room_item)
    print('You picked up {}'.format(room_item))

    #command for moving rooms
    elif player_input.split()[0] == 'Go':
    user_direction = player_input.split()[1]
    if user_direction in directions:
    if user_direction in rooms[current_room]:
    current_room = rooms[current_room][user_direction]
    else:
    print('You can\'t go that way!')
    print('---------------------')

    else:
    print('Invalid input')


    #lose condition for the game
    if current_room == 'Cerberus\'s Lair' and len(inventory) < 6:
    print('You have run into Cerberus without collecting all your items! Game over.')
    exit()

    #win condition for the game
    if current_room == 'Cerberus\'s Lair' and len(inventory) == 6:
    print('You have collected all the stolen items and defeated Cerberus. Congratulations!')
    exit()

    while True:
    main()

    Continue reading...

Compartilhe esta Página