Home > Enterprise >  Python text based game
Python text based game

Time:12-13

I am trying to write code in Python for a text based game for my class and am having several issues. First, for some reason, the code isn't even running now when I try to debug it. When it was working, every time I'd enter a command, it would always print multiple lines of 'Enter a valid move', whether or not the command was valid. I am also having trouble on how to collect the item from each room and add it to the inventory.

rooms = {
        'Living Room': {'north': "Parent's Room", 'east': "Kitchen",'south': 'Dining Room', 'west' : 'Office'},
        "Parent's Room": {'east': "Amir's Room", 'south' : 'Living Room', 'item' : 'cape'},
        "Amir's Room": {'west': "Parent's Room"},
        'Office': {'east': 'Living Room', 'item' : 'Toy Phone'}, 
        'Dining Room': {'north': 'Living Room', 'east': 'Garage', 'item': 'Toy Taco'},
        'Garage' : {'west': 'Dining Room', 'item': 'Toy Car'},
        'Kitchen' : {'west': 'Living Room', 'north' : 'Pantry', 'item' : 'Toy Fork'},
        'Pantry' : {'south' : 'Kitchen', 'item' : 'Chocolate'}
    }
direction = '' # Creates empty variable for direction.
location = 'Living Room' # Places user in default starting point
inventory = []

def show_status(): 
    ''' Shows player status and commands.'''

def main():
    # Keep game going until player enters 'exit'
    while True:
        print('----------------------------')
        print('You are in the {}.'.format(location))
        
        input = input('Enter a direction to move: ').strip().lower() # Take input from user to move them in desired direction.
        direction = input.split()

        # Check to see if user entered valid move.
        for command in rooms[location]:
            if direction == command:
                location = rooms[location][direction]
                print(direction)
            elif direction != command: 
                print('Enter a valid move')

CodePudding user response:

Actually there are many problems with your program, let's go quickly throught them.

First, to run this program you need to call main function somewhere or use

if __name__ == "__main__":
    # rest of your program

Second problem is the input function reads from stdin but here:

input = (
    input("Enter a direction to move: ").strip().lower()
)  # Take input from user to move them in desired direction.

You override it! You need to rename your variable to e.g. user_input

Next problem is

if direction == command:

If you think the condition should be satisfied but isn't and you are more advanced, try to learn how to use debugger. If not the print statement will be enough for start

print(command) # prints 'north'
print(direction) # prints ['north']

So we discovered another problem - indeed

direction = my_input.split()

split() creates list so you try to compare list to string which won't work in that case.

And to travel around all rooms you can loop over dict:

for room_name, subdict in rooms.items(): #here subdict is a dictionary
    for command, subroom in subdict.items():
        (do what you need)

CodePudding user response:

Your code isn’t running because the main() function is never called.

Add this to the very end of your Python file:

if __name__ == '__main__':
    main()

Other than that… your code, as is, has countless runtime bugs that make it impossible for the code to work as you intended it to. Just as an example, here are a few problems with your code that need to be fixed before you can do anything else:

  1. Add the previously mentioned code to call main()
  2. Make location accessible via the main() function, for example by adding global location as the first line of your main().
  3. Rename your input variable because you cannot reassign the built-in function called input(). Rename it to input_str, for example.
  4. Fix the logic inside of the very last if… else block: the elif block shouldn’t print “Enter a valid move” after every single iteration.
  • Related