大量if、elif、else语句导致问题

2024-04-19 23:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的游戏编写一些代码,我遇到了一个问题。如果这很难理解,我会提前道歉。第一部分很好。有大量的代码,所以我把它粘贴到codepad.org更容易分享。这是链接;http://codepad.org/kT8szBb2

108号线和142号线应该一起工作。我尝试过不同的方法,比如添加以下内容:

if True:

尝试重新定位缩进级别,但由于任何原因它似乎不起作用。任何建议都有效;我愿意尝试任何东西,即使这意味着重新编写整个片段。提前谢谢。在


Tags: 方法代码org定位truehttp游戏if
3条回答

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

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

这不是Python的工作方式。Python使用以下结构:

^{pr2}$

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

{{cd2}你真的需要一个循环。在

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

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

输出:

^{pr2}$

我冒昧地重构了您的代码以使其保持正常。在

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

相关问题 更多 >