Ad

Python Text Adventure Game, Ending Loop

- 1 answer

I am stuck on ending my game and breaking the loop. I have tried rephrasing, putting it in different spots, etc. The game-ending parameter is that if you reach the State of Florida with all six items you win, otherwise you lose. Instead, I can get to that final room and it either continues to prompt for input or announces that you've won the game even if you didn't.

print("\nMovement commands : North, South, East, or West")
print("Add to inventory: Get item\n")
introduction() # I just cut my long-winded intro. it works.

rooms = {
    'House': {'north': 'Drug Store', 'south': 'Clinic', 'east': 'Kitchen', 'west': 'Craft Store'},
    'Drug Store': {'south': 'House', 'east': 'Electronics Store', 'item': 'Hand Sanitizer'},
    'Electronics Store': {'west': 'Drug Store', 'item': 'ANC Headphones'},
    'Craft Store': {'east': 'House', 'item': 'A Mask'},
    'Clinic': {'north': 'House', 'east': 'CDC', 'item': 'A Vaccine'},
    'CDC': {'west': 'Clinic', 'item': 'Dr Fauci Candle'},
    'Kitchen': {'west': 'House', 'north': 'State of Florida', 'item': 'Anti-viral Spray'},
    'State of Florida': {'item': 'COVID-19'}  # VILLAIN, final room
}

current_room = 'House'  # location variable that will change as player moves
inventory = []  # empty list that will fill as you collect items
directions = ('north', 'south', 'east', 'west')  # possible movements
item = ('hand sanitizer', 'anc headphones', 'a mask', 'a vaccine', 'dr fauci candle',
        'anti-viral spray', 'covid-19') 

while True:
    print('\nYou are in the {}'.format(current_room)) # current game status
    print('Inventory: {}'.format(inventory))
    if 'item' not in rooms[current_room]:
        pass
    else:
        print('You see {}'.format(rooms[current_room]['item']))
    print('-' * 25)

    command = input('Enter your move:\n').lower().strip()

I initially put my game ending statement here, but it just loops again and then you're stuck in Florida forever because there is no room attached to it in the dictionary

if command in directions:
    if command in rooms[current_room]:
        current_room = rooms[current_room][command]
        if current_room in ['State of Florida']:
            if len(inventory) == 6:
                print('You have contracted COVID-19! G A M E  O V E R')
            else:
                print('You have defeated COVID-19!')
                print('Thank you for protecting your fellow teammates.')
            break

It has you win the game when you get here, regardless of inventory requirement (I have tried this as:) if len(inventory) == 6 and 'item' == 'COVID-19' print win if len(inventory) != 6 and 'item' == 'COVID-19' print lose

    else:
        print('You cannot go that way, please stop running into brick walls.')

elif command == 'get item':
    if 'item' in rooms[current_room]:
        print('{} retrieved! Gold star!'.format(rooms[current_room]['item']))
        inventory.append(rooms[current_room]['item'])
        del rooms[current_room]['item']
    else:
        print('FOCUS! There is nothing in here.')

else:
    print('What did you just call me? RUDE. Try again.')
Ad

Answer

In this code:

        if current_room in ['State of Florida']:
            if len(inventory) == 6:
                print('You have contracted COVID-19! G A M E  O V E R')
            else:
                print('You have defeated COVID-19!')
                print('Thank you for protecting your fellow teammates.')
            break

(aside: you should just do current_room == 'State of Florida' instead of using in)

your break statement will only break the inmost loop. If you only have one loop (that while True:), that should do the trick. Since it's not working, I suspect you have a loop instead that loop. The inner loop is the one that gets broken, after which execution contains with whatever comes after that loop (and then eventually back to the start of the outer loop).

If that's the case, an easy solution is to put all of this code into a function, so that you can return -- a return statement immediately exits the function regardless of how many levels of nested loop you're currently in.

Note also that if the intent is to win the game when you have six items, rather than lose the game, you should invert your if statement, e.g. make it so that if len(inventory) != 6 you lose, rather than the other way around (with the condition being == instead of !=) as it is in your current code

Ad
source: stackoverflow.com
Ad