我想比较两个strings
在python 3.6
,如果他们不相等,则打印消息并退出。我当前的代码是:
location = 'United States of America'
if location.lower() != 'united states of america' or location.lower() != 'usa':
print('Location was different = {}'.format(location.lower()))
sys.exit()
else:
#do something
但是上面的代码不能匹配两个字符串,即使它们相等,它也会进入循环并打印出不同的字符串。我知道我正在犯一些愚蠢的错误,但无法弄清楚。
您的情况:
if location.lower() != 'united states of america' or location.lower() != 'usa':
永远不会是False
,因为location.lower()
不能同时是2个不同的字符串。
我怀疑您想要:
if location.lower() != 'united states of america' and location.lower() != 'usa':
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句