0

I'm currently working on this text based game for a python introduction class. I can't figure out why some parts of the code takes me to another part that I didn't assign. Like I would go east then north, which should take me to 'Hidden Mud Warehouse', but it would take me to the beginning room instead. I know there's a lot of mistakes of inefficient way of coding there, but please just comment on how to fix the main problem for now. Thanks

def show_ins():

    print("NEUTRALIZE THE ATAKSUKI!")
    print("Team up with 6 teammates to stop the Ataksuki, or they will kill your dear friend")
    print("Move commands: go South, go North, go East, go West, Exit")
    print("Add teammate: get 'teammate name'")
def main():

    rooms = {
        'Hidden Mud Village Hall': {'South': 'The Clinic', 'West': 'The Flower Shop', 'North': 'The Bookstore',
                                    'East': 'Yoga House'},
        'The Clinic': {'North': 'Hidden Mud Village Hall', 'East': 'Weapons Depot', 'teammate': 'Lady Choyi'},
        'Weapons Depot': {'West': 'The Clinic', 'teammate': 'Ten'},
        'The Flower Shop': {'East': 'Hidden Mud Village Hall', 'teammate': 'Askura'},
        'The Bookstore': {'South': 'Hidden Mud Village Hall', 'East': 'The Gym', 'teammate': 'Akashi'},
        'The Gym': {'West': 'The Bookstore', 'teammate': 'Mighty Dude'},
        'Yoga House': {'West': 'Hidden Mud Village Hall', 'North': 'Hidden Mud Warehouse', 'teammate': 'Neju'},
        'Hidden Mud Warehouse': {'South': 'Yoga House', 'character': 'The Ataksuki'}
    }
    teammates = []
    while True:
        show_ins()
        currentroom = 'Hidden Mud Village Hall'
        command = ''
        while currentroom == 'Hidden Mud Warehouse':
            if teammates != ['Lady Choyi', 'Ten', 'Askura', 'Neju', 'Mighty Dude', 'Akashi']:
                print('You failed. Try again.')
            elif teammates == ['Lady Choyi', 'Ten', 'Askura', 'Neju', 'Mighty Dude', 'Akashi']:
                print('Good job! You saved Garaa!')
        while currentroom == 'Hidden Mud Village Hall':
            print('------------------------------------------------------------------------------------')
            print('You are currently in', currentroom)
            print('Teammates: {}'.format(teammates))
            command = input('Command:\n')
            if command not in ['go south', 'go South', 'go west', 'go West', 'go north', 'go North', 'go east', 'go East', 'exit','Exit']:
                print('------------------------------------------------------------------------------------')
                print('Invalid command!')
            elif command in ['exit', 'Exit']:
                currentroom = 'Exit'
                print('Coward! Come back here!')
                quit()
            elif command in ['go south', 'go South']:
                currentroom = 'The Clinic'
            elif command in ['go west', 'go West']:
                currentroom = 'The Flower Shop'
            elif command in ['go north', 'go North']:
                currentroom = 'The Bookstore'
            elif command in ['go east', 'go East']:
                currentroom = 'Yoga House'
        while currentroom == 'The Clinic':
            print('------------------------------------------------------------------------------------')
            print('You are currently in', currentroom)
            print('You see Lady Choyi')
            print('Teammates: {}'.format(teammates))
            command = input('Command:\n')
            if command not in ['go north', 'go North', 'go east', 'go East', 'exit', 'Exit', 'get lady choyi', 'get Lady Choyi']:
                print('------------------------------------------------------------------------------------')
                print('Invalid command!')
            elif command in ['exit', 'Exit']:
                currentroom = 'Exit'
                print('Coward! Come back here!')
                quit()
            elif command in ['get lady choyi', 'get Lady Choyi']:
                print('Lady Choyi has joined you')
                teammates.append ('Lady Choyi')
            elif command in ['go north', 'go North']:
                currentroom = 'Hidden Mud Village Hall'
            elif command in ['go east', 'go East']:
                currentroom = 'Weapons Depot'
        while currentroom == 'The Flower Shop':
            print('------------------------------------------------------------------------------------')
            print('You are currently in', currentroom)
            print('You see Askura')
            print('Teammates: {}'.format(teammates))
            command = input('Command:\n')
            if command not in ['go east', 'go East', 'exit', 'Exit', 'get askura', 'get Askura']:
                print('------------------------------------------------------------------------------------')
                print('Invalid command!')
            elif command in ['exit', 'Exit']:
                currentroom = 'Exit'
                print('Coward! Come back here!')
                quit()
            elif command in ['get askura', 'get Askura']:
                print('Askura has joined you')
                teammates.append ('Askura')
            elif command in ['go east', 'go East']:
                currentroom = 'Hidden Mud Village Hall'
        while currentroom == 'The Bookstore':
            print('------------------------------------------------------------------------------------')
            print('You are currently in', currentroom)
            print('You see Akashi')
            print('Teammates: {}'.format(teammates))
            command = input('Command:\n')
            if command not in ['go south', 'go South', 'go east', 'go East', 'get akashi', 'get Akashi']:
                print('------------------------------------------------------------------------------------')
                print('Invalid command!')
            elif command in ['exit', 'Exit']:
                currentroom = 'Exit'
                print('Coward! Come back here!')
                quit()
            elif command in ['get akashi', 'get Akashi']:
                print('Akashi has joined you')
                teammates.append ('Akashi')
            elif command in ['go south', 'go South']:
                currentroom = 'Hidden Mud Village Hall'
            elif command in ['go east', 'go East']:
                currentroom = 'The Gym'
        while currentroom == 'Yoga House':
            print('------------------------------------------------------------------------------------')
            print('You are currently in', currentroom)
            print('You see Neju')
            print('Teammates: {}'.format(teammates))
            command = input('Command:\n')
            if command not in ['go west', 'go West', 'go north', 'go North', 'get Neju', 'get neju']:
                print('------------------------------------------------------------------------------------')
                print('Invalid command!')
            elif command in ['exit', 'Exit']:
                currentroom = 'Exit'
                print('Coward! Come back here!')
                quit()
            elif command in ['get Neju', 'get neju']:
                print('Neju has joined you')
                teammates.append ('Neju')
            elif command in ['go west', 'go West']:
                currentroom = 'Hidden Mud Village Hall'
            elif command in ['go north', 'go North']:
                currentroom = 'Hidden Mud Warehouse'
        while currentroom == 'Weapons Depot':
            print('------------------------------------------------------------------------------------')
            print('You are currently in', currentroom)
            print('You see Ten')
            print('Teammates: {}'.format(teammates))
            command = input('Command:\n')
            if command not in ['go west', 'go West', 'exit', 'Exit', 'get ten', 'get Ten']:
                print('------------------------------------------------------------------------------------')
                print('Invalid command!')
            elif command in ['exit', 'Exit']:
                currentroom = 'Exit'
                print('Coward! Come back here!')
                quit()
            elif command in ['get ten', 'get Ten']:
                print('Ten has joined you')
                teammates.append ('Ten')
            elif command in ['go west', 'go West']:
                currentroom = 'The Clinic'
        while currentroom == 'The Gym':
            print('------------------------------------------------------------------------------------')
            print('You are currently in', currentroom)
            print('You see Mighty Dude')
            print('Teammates: {}'.format(teammates))
            command = input('Command:\n')
            if command not in ['go west', 'go West', 'exit', 'Exit', 'get mighty dude', 'get Mighty Dude']:
                print('------------------------------------------------------------------------------------')
                print('Invalid command!')
            elif command in ['exit', 'Exit']:
                currentroom = 'Exit'
                print('Coward! Come back here!')
                quit()
            elif command in ['get mighty dude', 'get Mighty Dude']:
                print('Mighty Dude has joined you')
                teammates.append ('Mighty Dude')
            elif command in ['go west', 'go West']:
                currentroom = 'The Bookstore'
main()
Samwise
  • 68,105
  • 3
  • 30
  • 44
redpat
  • 1

1 Answers1

0

The immediate problem you're hitting is that your while loop resets the current room at the beginning of each iteration:

    while True:
        show_ins()
        currentroom = 'Hidden Mud Village Hall'
        ...

What you probably want to do instead is set it before the loop, and allow the body of the loop to change currentroom in response to commands:

    currentroom = 'Hidden Mud Village Hall'
    while True:
        show_ins()
        ...

Re: I know there's a lot of mistakes of inefficient way of coding -- here's an unsolicited suggestion on how you could write code that lets you get from any room to any other room using only the information in the rooms dictionary (i.e. without needing to have a separate "if currentroom = 'room'" for each room):

command = input('Command:\n').lower()
if command[:3] == "go ":
    # this handles *any* "go" command in *any* room using the rooms dict
    d = command[3:]
    if d not in ["north", "south", "east", "west"]:
        print("Invalid direction!")
    elif d not in rooms[currentroom]:
        print(f"Can't go {d} from {currentroom}.")
    else:
        currentroom = rooms[currentroom][d]
    continue
# handle other commands
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • Also the `elif` should simply be `else`; there is no way for the condition to be false if the opposite `if` condition was. – tripleee Aug 14 '21 at 15:58
  • ... Though the condition checks for a particular _sequence_ where you would probably simply want to check if it has the same members, in any sequence; a common way to do that is to use a `set` instead of a `list`, or convert the `list` to a `set` and then compare. – tripleee Aug 14 '21 at 16:00
  • Furthermore, the code uses `while` in several places where clearly you mean `if`, or else the code within the loop should change the current room so that the `while` condition becomes untrue; otherwise, you have an endless loop. – tripleee Aug 14 '21 at 16:03