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
#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


Messages In This Thread
RE: Newbie: Help with variable selection in a class - by Slack86 - Apr-05-2020, 11:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable Types in a class nafshar 9 2,773 Oct-07-2022, 07:13 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 6,364 Jul-01-2022, 06:34 AM
Last Post: SharonDutton
  Calling a base class variable from an inherited class CompleteNewb 3 1,904 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Can we access instance variable of parent class in child class using inheritance akdube 3 14,209 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  newbie question....importing a created class ridgerunnersjw 5 2,852 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  Class variable / instance variable ifigazsi 9 4,642 Jul-28-2020, 11:40 AM
Last Post: buran
  Assignment of non-existing class variable is accepted - Why? DrZ 6 4,537 Jul-13-2020, 03:53 PM
Last Post: deanhystad
  Limiting valid values for a variable in a class WJSwan 5 3,969 Jul-06-2020, 07:17 AM
Last Post: Gribouillis
  Using variable from a class in another .py script keegan_010 4 3,230 Mar-25-2020, 07:47 AM
Last Post: keegan_010
  How to access class variable? instances vs class drSlump 5 3,577 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