質問

I'm trying to write part of an adventure game program in Python, and though I have most of it down, I'm stuck at trying to return a value declared as "True" at the end of one branch in the function chain. Basically, this is a fight against a monster, which you can win if you choose the right options. If you win, you obtain the bow that he was guarding. Here is the code of the fight:

#The first line imports all the text displayed in the game, saved in gametext.py
#For purposes of clarity, all text has been saved to variables imported by the file.
#Any variable that is not "HaveBow", "why" or declared by a raw_input() is actually text.

from gametext import *

def BG_fight():

    print BowGuardIntro
    print InitOptions

    BGfirstmove = raw_input('> ')

    if BGfirstmove == "1":
        spearfight()
    elif BGfirstmove == "2":
        dead(BGUnarmed1) 
    else:
        dead(BGUnarmed2)

def spearfight():        
    print GotSpear
    print SpearFight

    spearact = raw_input("> ")

    if spearact == "1":
        blindfight()
    elif spearact == "2":
        dead(SeeChest)
    elif spearact == "3":
        dead(SeeArms)
    else:
        dead(NoUseSpear)

def blindfight():
    print BlindFight

    followblow = raw_input("> ")

    if followblow == "1":
        print Victory
        HaveBow = True
        return HaveBow

    elif followblow == "2":
        dead(BlindArms)
    else:
        dead(BlindNoKill)

def dead(why):
    print why
    exit(0)

BG_fight()

(If people are interested, I can also produce the contents of the gametext file, though I would rather not as it is lengthy and has nothing to do with the problem I'm having)

As you can see, only one branch there offers the winning condition (which would give the HaveBow = True value that I want to return), and it is nested two functions deep and part of an if-statement. What I want to know is how I return that "HaveBow = True" value back outside the function chain, so it can be used in other parts of the game? If I try to code:

HaveBow = blindfight()
print HaveBow

at the end of the code and try to run it, it just makes me repeat that part of the game twice, before declaring "True". Same goes if I try BG_fight() instead of blindfight(). But I don't want that; I just want the "True" for "HaveBow" so I can use the condition "HaveBow = True" in other parts of the game.

In short, my question is whether or not it's possible to return a value from a nested function chain without repeating the code of that chain, and if so, how?

Thanks.

役に立ちましたか?

解決

You can only return a value, not a name and a value. In other words, you cannot "return HaveBow = True"; all you can do is return True. There's no way to return a value and at the same time assign it to a variable that exists outside the function. If you want to assign the value, you need to do it outside the function.

From what you say, it sounds like you want HaveBow to be a global variable that you can use anywhere in your program. In that case, you can change your code in blindfight to do:

if followblow == "1":
    global HaveBow
    print Victory
    HaveBow = True

Note that in this case you do not need to return the True value -- you just directly assign it to a global variable.

Ultimately there are better ways to structure your code, but those issues are out of the scope of this question. For the moment, I would suggest that, if you have global state like HaveBow that you want to be able to use "anywhere else in the game", you should make those global variables using global as I showed in my example. As you progress learning more programming, you will gradually learn why using global variables is not usually the best idea, but for the purposes of this program it is the simplest solution.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top