Reputation: 2791
I did this in a previous adventure using this code:
def lobby():
invalid_check == True
while invalid_check == True: #This "while" loops exists solely to loop the command sequence should the user enter a bogus command.
action = raw_input("> ")
if "use" and "door" in action:
print "You feel a sense of anxiousness as your hand lingers over the door knob, but your curiosity sets in -- you open the door and step through."
invalid_check == False # It is now 'False' and will therefore allow the user to progress to the next 'room' or function.
toy_room()
elif "use" and "lever" in action:
print "test"
elif "examine" in action:
print "Pretty simple here -- just a blue door and a red lever."
elif "inventory" in action:
inv()
else:
invalid()
invalid_check == True
I made a while loop to prompt the user as long as this was True and whenever I wanted to stop the input prompt action (say, after death or upon moving to a new room) I would set this to False.
My friend edited my code for me and said that this was redundant -- I was wondering what would be a more efficient way to keep my input coming up in every room (each room being its own Class) after a user has input an action?
Example:
input > examine door
"Blah blah door"
input > *<----- prompt shows up again *
Upvotes: 1
Views: 201
Reputation: 32597
I think you confuse assignment (=
) with comparison (==
). The correct code would look something like:
def lobby():
invalid_check = True
while invalid_check:
action = raw_input("> ")
if "use" and "door" in action:
print "You feel a sense of anxiousness as your hand lingers over the door knob, but your curiosity sets in -- you open the door and step through."
invalid_check = False # It is now 'False' and will therefore allow the user to progress to the next 'room' or function.
toy_room()
......
Upvotes: 1
Reputation: 730
Are you thinking something like this:
while playing {
if input is not None:
# Examine input
if input == "exit":
playing = False
# Wait for user to input
input = raw_input('input > ')
}
Upvotes: 0