大量的if,elif,else语句导致问题

时间:2014-01-07 22:12:19

标签: python python-2.7 if-statement

我正在为我的游戏制作一些代码而且我遇到了问题。如果这很难理解,我会事先道歉。第一部分工作正常。有大量的代码,所以我将其粘贴到codepad.org以便于共享。这是链接; http://codepad.org/kT8szBb2

第108和142行应该一起工作。我尝试了不同的东西,比如添加:

if True:

尝试重新定位缩进级别,但无论出于什么原因它似乎都不起作用。任何建议工作;我愿意尝试任何事情,即使这意味着重写整个细分市场。提前谢谢。

3 个答案:

答案 0 :(得分:5)

好的,我想我已经找到了这个问题。你不太明白缩进是如何工作的。您的代码如下所示:

if a:
    if b:
        if c:
A()
else:
    B()
    else:
        C()

这不是Python的工作原理。 Python使用以下结构:

if a:
    A()
elif b:
    B()
elif c:
    C()

我真的很想知道理解中的错误来自哪里,因为这是一些非常混乱的代码。

答案 1 :(得分:2)

我冒昧地重构你的代码是理智的。

def weaponsel():
    swep = None
    #again, I'm not really sure where this is coming from or what you're doing with it
    #so it's hard to say if you should be saving the contents of swep before you run
    #the function, possibly to return if you select /return/ at the first prompt?
    while swep is None:
        print "What weapon would you like to use?"
        if weapondict["s1"] == None:
                print "Error #1: No weapons in the backpack. Contact me (Karatepig) at /hashed out for security/ and make me aware of this error."
                print "I can return you to the beginning of this checkpoint or I can end the game. Type /return/ to return or /end/ to end."
                er1=raw_input()
                if er1.lower() == "end":
                        import sys
                        sys.exit()
                elif er1.lower() == "return":
                        return None
                else: 
                        print "Sorry, I don't understand."
                        er1d()
        for weapon in ['s1','s2','s3','s4','s5','s6','s7','s8']:
            if weapondict[weapon]:
                print("The weapon {} is available".format(weapondict[weapon]))
        # as a side note, this can probably also be:
        ## for weapon in weapondict.values():
        ##     print("The weapon {} is available".format(weapon))
        # but that depends on what weapondict looks like!
        # It should be easy to expand to "types" of weapons, as well
        # using something e.g.
        ## weapondict = {"Rusty Sword":Sword(dmg=3), "Sharpened Spear":Spear(dmg=7)}
        # and testing for type(Sword) or type(Spear) based on player class or etc.
        # but you'd need to build classes for this to work, e.g.
        ## class Weapon(object):
        ##     def __init__(self,dmg=1):
        ##         self.dmg = dmg
        ##
        ## class Sword(Weapon):
        ##     self.type = "Sword"
        ##     self.dmgType = "Slashing"
        ##
        ## class Spear(Weapon):
        ##     self.type = "Spear"
        ##     self.dmgType = "Thrusting"
        # then you can have slashing do more damage to lightly armored targets and
        # thrusting do more damage to heavily armored targets and etc. Even writing
        # methods to attack characters based on their equipped weapons. This is a
        # PRIME example of where OOP will get you big results fast!

        weapon=raw_input()
        if weapon.lower() not in weapondict.values():
            print "Sorry, I don't understand that.\n\n"
            continue

        print("You have selected the {}".format(weapon))
        swepd1 = raw_input("Is that what you want? ")
        if swepd1.lower() in ("y","yes"): swep = weapon

如果您有任何疑问,请随时提出。我实际上没有测试过这个,所以语法错误可能比比皆是。不过,我相当确定它按预期工作。作为旁注 - weapondict来自哪里?它不在你的代码中,而且很可能这个函数看不到它(除非你之前将它定义为global weapondict。)

答案 2 :(得分:0)

所有if都没有必要。你绝对应该使用for循环。

weapons = { 1:'sword', 2:'mace', 3:'bow'}

for wep in weapons:
    print('the {} is available'.format(weapons[wep]))

输出:

the sword is available
the mace is available
the bow is available