Reputation: 65
I'm writing a terrible text-based adventure, and I can't figure out how to break this loop. I'll try to post the relevant stuff here. Please tell me if my comments don't adequately explain what I'm trying to do.
chained = 1
finished = 0
# home() and club() act as rooms in the game
def home():
while chained == 1:
# there's a way to unchain yourself that works
chained = 0
while chained == 0:
print 'your are in the room'
input = raw_input('> ')
if 'exit' in input or 'leave' in input or 'club' in input:
current_room = 'club'
break
def club():
print "this works"
# These sort of move you around the rooms.
# current_room keeps track of what room you're in
current_room = 'home'
while finished == 0:
if current_room == 'home':
home()
if current_room == 'club':
club()
The expected behavior is that I would enter "exit" or "leave" or "club" into the input, the home() function would end, and the club() function would start. What actually happens is that the terminal just keeps printing "you are in the room" and keeps giving me the input.
I'll post my completely unabridged code if I must, but I'd rather not, as the actual adventure isn't exactly...professional.
Upvotes: 1
Views: 3864
Reputation: 12504
I think you need to do make the current_room
a global variable. Because the variable current_room
in home()
and the one in while finished
has different scopes.
Something like the following is what you are trying to achieve I guess. Look up on python variable scopes
chained = 1
finished = 0
current_room = 'home'
# home() and club() act as rooms in the game
def home():
global chained
# without the following line current_room has local scope and updating its value will not be
# reflected in the while at the end
global current_room
while chained == 1:
# there's a way to unchain yourself that works
chained = 0
while chained == 0:
print 'your are in the room'
input = raw_input('> ')
if 'exit' in input or 'leave' in input or 'club' in input:
current_room = 'club'
break
def club():
print "this works"
# These sort of move you around the rooms.
# current_room keeps track of what room you're in
while finished == 0:
if current_room == 'home':
home()
if current_room == 'club':
club()
Upvotes: 0
Reputation: 13723
Though I never really understood what the code was meant for here's solution that works. You haven't stated if the variable's were global or local and why would you use loops when simple if-else statements could be used
chained = 0
finished = 0
# home() and club() act as rooms in the game
def home():
global chained,current_room
if chained == 1:
# there's a way to unchain yourself that works
chained = 0
if chained == 0:
print 'your are in the room'
input = raw_input('> ')
if 'exit' in input or 'leave' in input or 'club' in input:
current_room = 'club'
club()
# Not sure if finished is a local or global variable
def club():
global finished,current_room
print "this is messy code!!"
# These sort of move you around the rooms.
# current_room keeps track of what room you're in
current_room = 'home'
if finished == 0:
if current_room == 'home':
home()
if current_room == 'club':
club()
home()
Upvotes: 0
Reputation: 60024
What break
is doing is breaking out of the loop in the home()
function. So when that breaks, it will go back to the beginning of
while finished == 0:
And will keep on repeating that input.
You have to also supply a break
after home()
(and club()
):
while finished == 0:
if current_room == 'home':
home()
break
if current_room == 'club':
club()
break
Your code is extremely messy, by the way. While loops shouldn't be used for things like this (except when you're trying to get an input of exit
or leave
)
You may as well get rid of the final while loop.
Upvotes: 4