April 2nd, 2013, 11:24 AM
Looping User Input for many different things?
I'm writing a text based adventure game in python and I need to know how to bring a user back to a certain spot. For example, if they continuously enter nonsense, I want to infinitely prompt them with '>>>' until they have a valid response. Here is some of the code:
So if they put in stats i want it to display stats then prompt them again with >>>. With this i also want the >>> prompt to abide by the SAME code for the function dark_room(). If they say 'inventory' i want to bring up their inventory again and again infinitely until they say 'open windows' to go to the next function. Or if they put in nonsense I want them to be prompted with '>>>' over and over again. I've tried while loops such as while True, as well as try: and except: but it doesn't seem to work the way i do it. It either brings an error or gives me an unwanted/unexpected result like running through a loop once then terminating.
print "You can barely see save for the faint light that streams from the boarded up windows. Perhaps you can OPEN them up."
if prompt_dark_room=='open window':
print "Any statement" dark_room2()
elif prompt_dark_room=='open windows':
print "Barely lifting your feet from the ground, you make your way over to the source of light. you eventually make it to the window and tear the boards away from the window."
Can you modify my example to show me how to do this/or at least be very specific as I am new to this.
April 2nd, 2013, 03:18 PM
The "while bad try again with different input" pattern is common. And recursively coding it looks smart but it's actually not smart because incorrect answers fill the stack. If you're on unix you'll see the stack dump if you command
$ yes nonsense| python text_adventure_game.py
Recursion is useful technique. You'll need to understand how the stack unwinds (termination---your recursion hasn't got termination). Here's a recursive version of towers of Hanoi. It displays the content of each tower after each transfer.
dark_room() # don't do this!
The recursion stops because it calls itself for diminished action. For a transfer of 1 it does not recur.
def transfer(source, sink):
def hanoi(n, source, sink, extra):
if n == 1:
elif 1 < n:
hanoi(n-1, source, extra, sink) # move n-1 blocks to the extra tower
hanoi( 1, source, sink, extra) # move the big block to the destination
hanoi(n-1, extra, sink, source) # move the n-1 blocks from the extra onto the destination.
towers = (list(reversed(range(4))), , )
Hmm. Back to your question. How should it work? Hard to say. You should probably redesign your program. If "inventory" and "stats" should work the same in most rooms then it doesn't make sense to handle these requests in each room function. Here's my essentially useless and too difficult to understand answer:
answer = sys.stdin.readline()
if verify(answer, *args, **kwargs):
def verify(answer, room, character):
answer_lower_case = answer.lower()
(verb, noun, *junk) = (answer_lower_case+' verb noun junk').split() # assume python 3
if verb in ('inventory', 'i'):
elif (verb in ('get', 'take')) and (noun in room):
[/code] are essential for python code and Makefiles!
April 2nd, 2013, 07:58 PM
When you're right you're right. Like I said, I'm a beginner so when you have stuff like kwargs,sys.stdout and all I don't understand it. Also I don't mind reiterating 'stats' and 'inventory' each time. This is the first game I am making so if there is an easy way, I usually take the easy way and don't mind the extra work. But if there is any simpler way (perhaps that I can understand) that would be nice. Basically I just want to be able to say "If you don't put in any of the options I have defined, you get the prompt again (aka prompt_dark_room=raw_input('>>> '). Also I may have forgotten to mention I am using Python 2.7.