简体   繁体   中英

Large amount of if, elif, else statements causing issues

I am working on some code for my game and I am having an issue. I apologize in advance if this is hard to understand. The first section works fine. There is a large amount of code so I pasted it into codepad.org for easier sharing. Here's the link; http://codepad.org/kT8szBb2

Lines 108 and 142 are supposed to work together. I've tried different things like adding in this:

if True:

to try and reposition the indent level but for whatever reason it doesn't seem to work. Any suggestions work; I'm willing to try anything even if it means re-writing the entire segment. Thanks in advance.

Okay, I think I've found the issue. You don't quite understand how indentation works. You have code that looks like this:

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

This isn't how Python works. Python works with the following structure:

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

I'd really like to know where that mistake in understanding came from, because this is some extremely messy code.

I took the liberty of refactoring your code to be sane.

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

If you have any questions, don't hesitate to ask. I haven't actually tested this, so syntax errors may abound. I'm fairly certain it works as intended, however. As a side note -- where does weapondict come from? It's not in your code anywhere and it's likely that this function can't see it (unless you defined it earlier as global weapondict .)

There is really no need for all those if 's. You should absolutely be using a for loop.

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

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

outputs:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM