0

yeah, I'm just keeping this thread open because I have another wrinkle.

In my code

def prompt_house():
    global gold
    prompt_hou = raw_input('>')
    if prompt_hou == 'examine table':
        print '''There are a lot of car magazines here. You flip through them and find
5 gold.'''
        gold = gold+5
        prompt_house()

(or any other code where I get gold for that matter)

If I examine the table more than once, for each time I examine it I get 5 more gold. How do I fix this?

3
Contributors
8
Replies
9
Views
9 Years
Discussion Span
Last Post by IU34
0

Do not use recursion as a loop, but use a while loop in the function itself. You also need to set a flag that the gold has been taken. Further note that a triple quoted string relaxes the indentation rule:

def prompt_house():
    global gold
    gold_taken = False
    while True:
        prompt_hou = raw_input('>')
        if prompt_hou == 'examine table' and not gold_taken:
            print \
'''There are a lot of car magazines here.
You flip through them and find 5 gold.
'''
            gold = gold+5
            gold_taken = True
        elif prompt_hou == 'go west':
            # this gets you out of the loop
            go_west()
        # more elif choices here ...
            

def go_west():
    # just a dummy funk
    global gold
    print gold
    pass

# test
gold = 0
prompt_house()
0

okay, I tried that just now...

def prompt_house():
    global gold
    gold_taken = False
    while True:
       prompt_hou = raw_input('>')
    if prompt_hou == 'examine table' and not gold_taken:
        print  '''There are a lot of car magazines here.
you flip through them and find 5 gold.'''
        gold = gold+5
        gold_taken = True
        prompt_house()

(I tried putting it in there like you had it but it gave me syntax errors)

So, when I tried to run this program, go to the house and examine the table, the program just stopped responding. I could still type stuff in, and the was still the single '>' there, but it just didn't print anything or do anything...

by the way, how would I do more than one case of getting gold in the same room?

0

In line 11 of your code you still go back to the originating function, that's called recursion. You should not do that!

0

How do I fix it to where I can prevent recursion but still be able to do what was done under recursion?

0

Okay, solved.

and note for ZZucker's code:

After writing the other commands, you have to write a command for

elif prompt_hou == 'examine table' and gold_taken:
          print '''There are a lot of car magazines here.'''
          go_west()

and that works.

now i have a lot of re-writing to do...

::edit::
quick question: would writing something like this

def prompt_clear():
    prompt_cle = raw_input('>')
    if prompt_cle == 'south':
        field()
    elif prompt_cle == 'help':
        print 'look, examine (object), north, south, east, west'
        prompt_clear()
    elif prompt_cle == 'read sign':
        print """The sign says: Behold the mysterious grating! For some unknown reason, it will not open until you have 57 gold.
Why the odd number? I don't know...just play along will ya? You won't get out unless you do!"""
pass1()

def pass1():
global gold
prompt_clear()

work or would that still be recursion?

0

*sigh*

I've got to learn to be patient and try stuff before I post...
sorry for spamming this thread with so many posts.

0

okay, now for some odd reason, when I ported ZZucker's code to mine, I get the same problem as before...I can get the gold in the same spot over and over and over.
My code:

def prompt_house():
    global gold
    gold_taken = False
    while True:
        prompt_hou = raw_input('>')
        if prompt_hou == 'examine table' and not gold_taken:
            print  '''There are a lot of car magazines here.
you flip through them and find 7 gold.'''
            gold = gold+7
            gold_taken = True
            pass3()

     while False:
         prompt_hou = raw_input('>')
         if prompt_hou == 'examine table' and gold_taken:
            print 'There are a lot of car magazines here.'
            pass3()

and Zzucker's code:

def prompt_house():
    global gold
    gold_taken = False
    while True:
        prompt_hou = raw_input('>')
        if prompt_hou == 'examine table' and not gold_taken:
            print \
                  '''There are a lot of car magazines here.
You flip through them and find 5 gold.
'''
            gold = gold+5
            gold_taken = True
        elif prompt_hou == 'go west':
            # this gets you out of the loop
            go_west()
            # more elif choices here ...
        elif prompt_hou == 'examine table' and gold_taken:
            print '''There are a lot of car magazines here.'''
            go_west()
def go_west():
# just a dummy funk
    global gold
    print gold
    pass
                # test
gold = 0
prompt_house()

In Zzuvker's code, I can only get the gold ONCE, and once only. But in mine, infinite! Why??

::edit:: I have tried without the false case in mine, I have tried without the pass4()....but nothing makes a difference! it still lets me get the gold more than once!! Can somebody help please?

::edit2::

I have modified the code to this:

def prompt_kitchen():
    global gold
    gold_taken = False
    while True:
        prompt_kit = raw_input('>')
        if prompt_kit == 'examine cabinet 1' and not gold_taken:
            print \
                  '''This cabinet has a lot of cups in it with all different
designs and shapes. Where are the people anyway? How come there's nobody here?
In one of the cups you find 8 gold.
'''
            gold = gold+8
            gold_taken = True
        elif prompt_kit == 'examine cabinet 1' and gold_taken:
            print \
                  '''This cabinet has a lot of cups in it with all different
designs and shapes. Where are the people anyway? How come there's nobody here?'''
            pass4()

but still no go...

ah-ha! looks like something's up with the beginning...because I added a pass4()

def pass4():
    global gold
    print 'You have', gold, 'gold'
    pass

to the end of the first gold case, and after I examined cabinet one (twice) the second time it switched over to the gold_taken case...but after I went south and then north again, it switched _back_ over to the not gold_taken case...

0

So does anyone know how to fix that problem in te post above?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.