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


Messages In This Thread
RE: Newbie: Help with variable selection in a class - by Slack86 - Apr-06-2020, 10:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable Types in a class nafshar 9 2,774 Oct-07-2022, 07:13 PM
Last Post: deanhystad
  can Inner Class reference the Outer Class's static variable? raykuan 6 6,365 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