当我尝试运行此操作时,我收到语法错误,并且它没有说明它是什么行或什么。我不知道还能说些什么..以下是获得错误的代码:
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.")
顺便说一下,技能1,技能2和技能3都是我制作的游戏中不同技能的列表,技能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)
答案 0 :(得分:0)
你不能将elif嵌入if:
中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:
您的其余代码语法很好,只需将范围更改为>
。
答案 1 :(得分:0)
你的代码存在很多问题。例如:
if random.randint(1,100) != range(1,21):
你在这里做的是比较一个整数(1到100之间的随机数)和一个列表(范围的输出,[1,2, ..., 20
)。你的意思可能是not (... in range(...))
;这是好的,但是检查数字是否在两个其他数字之间的最耗时和耗费内存的方法。但是,这不是语法错误。
然而,这里的要点是你没有正确地缩进;您的elif
必须与相应的if
具有相同的缩进深度。
答案 2 :(得分:0)
作为IDLE的输出对我来说:
unindent与任何外部缩进级别都不匹配
您没有正确使用缩进。
检查以下内容:
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:名称'攻击'未定义
如果攻击是字符串而不是变量,则必须将其替换为“attack”(添加引号)
正如@Padraic Cunningham在评论中所说,攻击显然是一个变量!所以你必须定义它。 :)