【发布时间】:2017-11-26 06:55:08
【问题描述】:
我正在尝试进行 Reddit 挑战,但它似乎卡在了 if 声明上,即使是错误的也会播放。
type1 = input()
type2 = input()
if type1[:3] == 'nor':
if type2[:3] == 'nor'or'fir'or'wat'or'ele'or'gra'or'ice'or'fig'or'poi'or'gro'or'fly'or'psy'or'bug':
print('Normal 100% Damage')
elif type2[:3] == 'roc' or 'ste':
print('Not very effective 50% Damage')
elif type2[:3] == 'gho':
print('No effect 0% Damage')
else:
print('Not a valid type')
【问题讨论】:
-
因为 if 语句不是假的,也永远不会是假的。非空字符串被解析为
true,type2[:3] == 'nor'之后的项目不假设您正在与type2[:3]进行比较。相反,它将字符串解析为条件。请参阅this question,了解如何将一个项目与 if 语句中的多个值进行比较。 -
叹息......其中另一个......这是一个可悲的常见错误。请仔细阅读有关这些运算符如何工作的文档。
标签: python windows python-3.x if-statement pycharm