当我尝试运行此代码时,出现语法错误
当我尝试运行此代码时,出现语法错误,但未说明它是什么行。我不知道我能说些什么..这里是代码,获得误差位:当我尝试运行此代码时,出现语法错误
if "q" in attack:
if random.randint(1,100) != range(1,21):
print("You hit with a quick attack!")
ehp -= 20
print("The",enam,"loses 20 damage! It now has",ehp,"health.")
else:
print("You missed.. :(")
elif "p" in attack:
if random.randint(1,100) != range(1,51):
print("You hit with a power attack!")
ehp -= 50
print("The",enam,"loses 50 damage! It now has",ehp,"health.")
else:
print("You missed.. :(")
elif "1" in attack:
if mana >= skill1[2]:
print("You hit with",skill1[0])
ehp -= skill1[1]
mana -= skill1[2]
print("The",enam,"loses",skill1[1],"damage! It now has",ehp,"health.")
print("You now have",mana,"mana.")
elif "2" in attack:
if mana >= skill2[2]:
print("You hit with",skill2[0])
ehp -= skill2[1]
mana -= skill2[2]
print("The",enam,"loses",skill2[1],"damage! It now has",ehp,"health.")
print("You now have",mana,"mana.")
elif "3" in attack:
if mana >= skill3[2]:
print("You hit with",skill3[0])
ehp -= skill3[1]
mana -= skill3[2]
print("The",enam,"loses",skill3[1],"damage! It now has",ehp,"health.")
print("You now have",mana,"mana.")
else:
print("You typed something wrong.")
顺便说一句,skill1,skill2和skill3是在比赛中我要作不同的技能所有列表技能1 [0]是技能的名称,技能[1]是技能的攻击力和技能[2]是用来使用技能的法力值。
skill1 = []
skill2 = []
skill3 = []
skill1.append("Very Weak Fireball")
skill1.append(20)
skill1.append(30)
skill2.append("Weak Fireball")
skill2.append(30)
skill2.append(40)
skill3.append("Average Fireball")
skill3.append(40)
skill3.append(50)
不能嵌套的,如果里面的elif:
if "q" in attack: # in line with the elif's
if random.randint(1,100) > 21: # cannot compare to range use >
print("You hit with a quick attack!")
ehp -= 20
print("The",enam,"loses 20 damage! It now has",ehp,"health.")
else:
print("You missed.. :(")
elif "p" in attack:
if random.randint(1,100)> 51: # greater that 51
print("You hit with a power attack!")
ehp -= 50
print("The",enam,"loses 50 damage! It now has",ehp,"health.")
else:
print("You missed.. :(")
elif "1" in attack:
您的代码语法的其余部分都很好,只是改变范围>
。
你的代码有太多问题。例如:
if random.randint(1,100) != range(1,21):
你在这里做什么比较列表(范围的输出,这是[1,2, ..., 20
)一个整数(1到100之间的随机数)。你的意思可能是not (... in range(...))
;这是可以的,但是检查数字是否在两个其他数字之间的时间和内存消耗最多。但是,这不是语法错误。
但是,这里的要点是,你不能正确缩进;您的elif
必须具有与相应的if
相同的缩进深度。
空闲的输出对我来说:
unindent does not match any outer indentation level
你没有使用正确的压痕。
检查以下:
if "q" in attack:
if random.randint(1,100) != range(1,21):
print("You hit with a quick attack!")
ehp -= 20
print("The",enam,"loses 20 damage! It now has",ehp,"health.")
else:
print("You missed.. :(")
elif "p" in attack:
if random.randint(1,100) != range(1,51):
print("You hit with a power attack!")
ehp -= 50
print("The",enam,"loses 50 damage! It now has",ehp,"health.")
else:
print("You missed.. :(")
elif "1" in attack:
if mana >= skill1[2]:
print("You hit with",skill1[0])
ehp -= skill1[1]
mana -= skill1[2]
print("The",enam,"loses",skill1[1],"damage! It now has",ehp,"health.")
print("You now have",mana,"mana.")
elif "2" in attack:
if mana >= skill2[2]:
print("You hit with",skill2[0])
ehp -= skill2[1]
mana -= skill2[2]
print("The",enam,"loses",skill2[1],"damage! It now has",ehp,"health.")
print("You now have",mana,"mana.")
elif "3" in attack:
if mana >= skill3[2]:
print("You hit with",skill3[0])
ehp -= skill3[1]
mana -= skill3[2]
print("The",enam,"loses",skill3[1],"damage! It now has",ehp,"health.")
print("You now have",mana,"mana.")
else:
print("You typed something wrong.")
后,如果您没有定义攻击,您将收到另一个错误:
NameError: name 'attack' is not defined
如果攻击是一个字符串不是一个变量,您必须将其替换为“攻击”(加引号)
由于@Padraic坎宁安在评论中说,攻击显然是个变数!所以你必须定义它。 :)
q怎么会发生“攻击“? – 2015-02-07 10:22:44
@PadraicCunningham它不能! :)等python不运行在其块中的代码!我再次检查!它不会运行任何块! :D – Jean 2015-02-07 10:27:27
那么代码将永远不会运行,我认为这是安全的假设攻击是一个变量;) – 2015-02-07 10:28:31
哪个语法错误?它通常给你的行号是错误的,这在这里会非常有用。但是既然你说的没有,确切的错误确实会变得更有趣 – 2015-02-07 10:07:50
请确保你正确地缩进了你的代码(第一个'if'语句) – vaultah 2015-02-07 10:09:06
请注意你的缩进.. – Olu 2015-02-07 10:14:12