Python Forum
Newbie: Help with variable selection in a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie: Help with variable selection in a class
#1
Hi there.

Learning Python with my daughter, and we're looking for some help with her project.

Code below:
class room():
    
    def __init__(self, desc, exit, moveto):
        self.desc = desc
        self.exit = exit
        self.moveto = moveto

door = room('You see a door', 'entrance', 1)
entrance = room("You are in the entrance", 'door', door)
hall = room('You are in the Hall', 'entrance', entrance)
bedroom = room('You are in a bedroom', 'hall', hall)
currentRoom = bedroom

def playerAction():
    print(currentRoom.desc)
    print('Exits:', currentRoom.exit)
    
while True:
    playerAction()    
    action = input('What do you do? >')
    if action == currentRoom.exit :
        print(action + 'Entered')
        currentRoom = currentRoom.moveto
    else :
        print('No')
Question: When trying to move back a room, say from hall to bedroom, it gives an error that currentRoom is not defined. I'm guessing the variables in the Room class are defined in order : how would we be able to move from "entrance" to "hall".

Thanks
Reply
#2
Appreciate the effort, but your tags did not work to show this as code. You need to use the button (beside Bold, Italic, etc.)
Still I think I see what is going on. In your program logic it looks like you can only go to one place from each location. From hall you can go to entrance but no where else, from what I see.
Your rooms are a linked list. From one instance you can go to another. It needs to be a double linked list in order to allow movement in both directions. Alternatively, you could put your movements on a stack (a special kind of list) and when the user types "back" you pop the last room off the stack and it becomes the current room, essentially retracing your steps.

Not sure that answers your question, so if you can clarify a little it would help.
J
Reply
#3
A for effort. Let me try that again.

what you're saying is basically it : i can only move rooms in cadence. Now if I added a second moveto instance, say
class room():
...
self.moveto2 = moveto2

entrance = room("You are in the entrance", 'door', door, entrance)

If i try to move from entrance to door, the error still says that I have not defined door. I'm assuming my entire logic and the way I'm using the class is wrong. I understand that technically door comes before entrance, so it sees currentRoom as being door and stops. Just not sure if I can make it keep going down the variable list as is to see the variable after the one that's used.

Now if I were to want to go from door to bedroom, let's say, I wouldn't be able to use a "back to previous room" function, so with my current layout that would be impossible to do?

class room():
    
    def __init__(self, desc, exit, moveto):
        self.desc = desc
        self.exit = exit
        self.moveto = moveto

door = room('You see a door', 'entrance', 1)
entrance = room("You are in the entrance", 'door', door)
hall = room('You are in the Hall', 'entrance', entrance)
bedroom = room('You are in a bedroom', 'hall', hall)
currentRoom = bedroom
#End room definitions
#Defintes function for and prints what you see on the screen
def playerAction():
    print(currentRoom.desc)
    print('Exits:', currentRoom.exit)
    print('************************')
    
while True:
    playerAction()    
    action = input('What do you do? >')
#Defines what to do with the inputs
    if action == currentRoom.exit :
        currentRoom = currentRoom.moveto
    else :
        print('No')
Reply
#4
Update on this issue:

I had to create a dictionary and add the rooms to it, then call the room function at my if statement.

class room():
    
    def __init__(self, desc, exit, details):
        self.desc = desc
        self.exit = exit
        self.details = details

world = {}
world['door'] = room('You see a door.', 'entrance', 'Test')
world['entrance'] = room("You are in the entrance.", 'door', 'A white door blocks your way.')
world['hall'] = room('You are in the Hall.', ('entrance', 'bedroom'), 'The hardwood floor is scuffed and there is a mirror on the wall.')
world['bedroom'] = room('You are in a bedroom.', 'hall', 'There is only an old metal framed bed and a worn dresser.')
currentRoom = world['bedroom']

def playerAction():
    print(currentRoom.desc)
    print('Exits:', currentRoom.exit)
    print('************************')
    time.sleep(0.5)

while True:
    playerAction()    
    action = input('What do you do? >')
#Defines what to do with the inputs
    if action in currentRoom.exit :
        currentRoom = world[action]
Reply
#5
There are different ways to approach this. One way that would add flexibility is to have exit be a list. That way a room like 'hall' could have options of 'bathroom', 'bedroom1', 'bedroom2', and 'stairs'. Once in bedroom1 the exits could be 'hall' and 'attic'. With this, you could have some twists and turns, make it so entering bedroom2 does not go back to the hall - the door locked behind you, so you have to go through some other route.

You would accomplish movement by seeing if the destination is in the exit list. If it is, that becomes the current room.

Just an idea.
Reply
#6
Idea on how to do and you can expand upon - modified your code to allow a list of exits. Changed init to just have the name and description, defined the rooms, then added the exits to each room. Trying to put the exits into init was problematic as rooms would reference other rooms that had not been created yet. Had to make the responses check against the name field. If you run it, can run around using the different connections.

class room():
     
    def __init__(self, desc, name):
        self.desc = desc
        self.name = name
        
 
# Define the rooms
door = room('You see a door','door')
entrance = room("You are in the entrance",'entrance')
hall = room('You are in the Hall','hall')
bedroom = room('You are in a bedroom','bedroom')

# Make the map
door.exit = [entrance]
entrance.exit = [door,hall]
hall.exit = [entrance,bedroom]
bedroom.exit = [hall]
currentRoom = bedroom
 
def playerAction():
    print(currentRoom.desc)
    print('Exits:')
    for ruhm in currentRoom.exit :
        print(ruhm.name)
     
while True:
    playerAction()    
    action = input('What do you do? >')
    moved = False
    for ruhm in currentRoom.exit :
        if (action) == ruhm.name :
            print(action + 'Entered')
            currentRoom = ruhm
            moved = True
        
    if not moved :
        print('No')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable Types in a class nafshar 9 2,450 Oct-07-2022, 07:13 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 5,884 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Calling a base class variable from an inherited class CompleteNewb 3 1,676 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,977 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  newbie question....importing a created class ridgerunnersjw 5 2,636 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  Class variable / instance variable ifigazsi 9 4,301 Jul-28-2020, 11:40 AM
Last Post: buran
  Assignment of non-existing class variable is accepted - Why? DrZ 6 4,264 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  Limiting valid values for a variable in a class WJSwan 5 3,601 Jul-06-2020, 07:17 AM
Last Post: Gribouillis
  Using variable from a class in another .py script keegan_010 4 2,981 Mar-25-2020, 07:47 AM
Last Post: keegan_010
  How to access class variable? instances vs class drSlump 5 3,333 Dec-11-2019, 06:26 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020