[英]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. 有大量的代码,因此我将其粘贴到codepad.org中以便于共享。 Here's the link; 这是链接; http://codepad.org/kT8szBb2 http://codepad.org/kT8szBb2
Lines 108 and 142 are supposed to work together. 线路108和142应该一起工作。 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的工作方式。 Python works with the following structure: Python具有以下结构:
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? 附带说明- weapondict
是从哪里来的? 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
.) 它不在您的代码中任何地方,并且很可能此函数看不到它(除非您之前将其定义为global weapondict
。
There is really no need for all those if
's. 真的不需要所有的if
。 You should absolutely be using a for
loop. 您绝对应该使用for
循环。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.