[Python] My code is running both my if statement and my else statement at the same time?

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

  1. Stack

    Stack Membro Participativo

    I'm working on a text-based game. My code works fine, but the else statement at the very end of the code prints even when my if statement is true. How can I fix it? The else: print("Invalid input") executes no matter what input is input id like it to only show when the win/lose if statements are false

    def show_instructions():
    print('---------------------------'),
    print("Welcome to the Zombie Apocalypse Text Game"),
    print("Collect all 6 items, or lose to a zombie hoard"),
    print("Your move commands are: 'North, East, South, West'"),
    print("Add an item to your inventory: Get 'Item Name'"),
    print('---------------------------')

    def player_status():
    print(inventory)

    def main():
    player_status()
    rooms={
    'Reception':{'East':'Interview Room', 'South':'Intake'}, #starting room
    'Interview Room':{'West':'Reception', 'item':'ammo'},
    'Intake':{'West':'Holding Cell','South':'General Population','East':'Equipment Room',
    'North':'Reception','item':'flash bang'},
    'Holding Cell':{'East':'Intake', 'item':'handgun'},
    'General Population':{'North':'Intake', 'East':'Yard', 'item':'shank'},
    'Equipment Room':{'West':'Intake', 'North':'Wardens Office','item':'riot gear'},
    'Wardens Office':{'South':'Equipment Room', 'item':'shotgun'},
    'Yard':{'West':'General Population', 'villain':'zombies'}} #Zombies
    starting_room = 'Reception'
    current_room = starting_room
    directions = 'north', 'east', 'south', 'west'
    items =[{'Reception':'None'}]
    inventory = []
    while True:
    show_instructions()
    if 'item' in rooms[current_room]:
    items = rooms[current_room]['item']
    print('you see {}'.format(items))
    print('\nYou are currently in {}'.format(current_room))
    print(inventory)
    # players input, IE: directions or picking up items
    user_input = input(f'Enter your move command: ').capitalize().split()
    if user_input[-1] == 'Exit':
    print("Thank you for playing")
    break
    elif user_input[-1] in rooms[current_room]:
    user_input = user_input[-1]
    current_room =rooms[current_room][user_input]
    # players get item input
    elif user_input[0] == 'Get':
    item = ' '.join(user_input[1:])
    if 'item' in rooms[current_room] and item == rooms[current_room]['item']:
    inventory.append(item)
    else:
    print('Invalid command')
    # lose condition
    if current_room == 'Yard' and len(inventory) < 6:
    print("You weren't able to collect all the items")
    print("You weren't able to clear the yard")
    # win condition
    elif current_room == 'Yard' and len(inventory) > 6:
    print("CONGRATS!! You were able to collect all items and")
    print("You were able to clear the yard")
    # if an input other than get or direction is input
    else:
    print("Invalid input") <---- this outputs

    Continue reading...

Compartilhe esta Página