0

Hello I am new to python and this is my first stack overflow post, so I apologize if this question has been answered elsewhere (I'm not entirely sure how to search for this topic, feel free to drop a link if it is).

I'm creating an adventure game and I want the user to choose different paths within a house.

I want the user to be able to travel back and forth between room A and room B, however I want different dialogue to appear based on how many times a user has entered room A or room B. If its the first time, I want the function to print (option 1), but if the user enters >1 time i want the function to print (option 2)

I've figured out how to travel back and forth between the 2 rooms, but I don't get different dialogue options if I enter either room >1 times with my current increment code. Does it have to do with the fact that I exit the room_A function when i 'travel to room_B'?

Any help would be greatly appreciated!

def room_A():
     room_A_count = 0

# - if this is the first time the user enters the room, this code runs...
     if room_A_count == 0:
          print ("You enter room A")
          print ("What would you like to do?")
          print ("1. Enter room B")
          print ("2. nothing")
          room_A_choice = input("Choice: ")

          if room_A_choice = "1":
               room_A_count += 1
               # - travel to room B
               room_B()
          elif room_A_choice == "2":
               print ("You do nothing")

# - if the user travels back from Room B to Room A, it prints this message...
     else: 
          print ("You enter room A again") 

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
jwross
  • 3
  • 1

3 Answers3

1

One thing I'd say is that a good principle to generally follow is for functions to behave the same way every time you call them. With that being said, one work around could be to store a room_A_count as a global variable. Then pass it into the function as an argument and increment each time.

That's a bit hacky and seems like it could break easy, so I'd propose an object oriented solution-

class RoomA:
    def __init__(self):
        self.room_A_count = 0

    def enter(self):
        if self.room_A_count == 0:
          print ("You enter room A")
          print ("What would you like to do?")
          print ("1. Enter room B")
          print ("2. nothing")
          room_A_choice = input("Choice: ")

          if room_A_choice = "1":
               self.room_A_count += 1
               # - travel to room B
               room_B()
          elif room_A_choice == "2":
               print ("You do nothing")

        else: 
            print ("You enter room A again")
MacNutter
  • 182
  • 1
  • 5
  • thank you for the prompt response! I think this is the route that serves me purpose. Just a follow up question. How do you activate the "class roomA" so it starts the back and forth cycle for the user to travel back and forth between rooms? – jwross Nov 10 '19 at 16:37
  • So you'd need to instantiate an object of the class `a = RoomA()`. Then you could run the method via `a.enter()`. If you're not familiar with OOP, maybe [this](https://www.datacamp.com/community/tutorials/python-oop-tutorial) can get you started. – MacNutter Nov 11 '19 at 17:24
  • Awesome thank you for taking the time to help with this and point me in the right direction for further education! – jwross Nov 12 '19 at 14:24
0

I'm unable to comment, so I apologize if I understand your question right, all you'll need to do is add an if statement where it checks for room_A_choice

          if room_A_choice = ="1":
               room_A_count += 1
               if room_A_count >= 2:
                    # do something
               else : room_B()

HostingUdp
  • 52
  • 4
  • thank you for your response! This is currently the method I am using and it is not working as desired. The "room count" does not increment when traveling back and forth between room A and room B – jwross Nov 10 '19 at 16:41
0

You can use function static variables

def room_A():
  if not hasattr(room_A, 'room_A_count'): # check if static variable exists
    room_A.room_A_count = 0               # initialize if doesn't set

  if room_A.room_A_count == 0:  # refers to the funtion static variable
    print ("You enter room A")
    print ("What would you like to do?")
    print ("1. Enter room B")
    print ("2. nothing")
    room_A_choice = input("Choice: ")

    if room_A_choice == "1":
          room_A.room_A_count += 1 # incremention function static variable
          # - travel to room B
          room_B()
    elif room_A_choice == "2":
          print ("You do nothing")

    # - if the user travels back from Room B to Room A, it prints this message...
  else: 
    print ("You enter room A again") 

def room_B():
  print('In room B')

room_A()                 # First call
room_A()                 # Second call

Output

You enter room A
What would you like to do?
1. Enter room B
2. nothing
Choice: 1
In room B
You enter room A again
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • thank you for the prompt response! I tried this method and was able to get an alternate message the 2nd time I entered room A. (I altered my code to allow the user to enter Room B again) but was unable to then transition back to Room B. This worked great for a one-time alternate response though – jwross Nov 10 '19 at 16:39
  • @jwross--not sure what you meant by "(I altered my code to allow the user to enter Room B again) but was unable to then transition back to Room B". Are you saying that if you enter B from A, then you want to have the option in the future of again entering B from A (i.e. execute code where room_A.room_A_count == 0)? – DarrylG Nov 10 '19 at 17:07
  • sorry that was unclear, all I meant was I added a second function (named room_B()) nearly identical to the first, so I could execute Function room_A(), then select choice 1 to travel to Function room_B(), then select choice 1 again to travel to Function room_A(), back and forth. I tried your code but it did not allow me to "travel" back and forth between rooms and print different outputs based on how many times I had previously visited room_A or room_B. Let me know if this clears up any confusion or if i need to study Python vocabulary more :) – jwross Nov 10 '19 at 21:13