【发布时间】:2015-10-20 18:19:07
【问题描述】:
所以我使用了下面的代码,它一直评估为 False,但它是 True。作为一个 Python 2.7 菜鸟,我不知道为什么。
s = 'Z_15'
if s.startswith('Z_') & int(s[2:]) >= 15:
new_format = True
else:
new_format = False
print new_format
还有这个变化:
s = 'Z_15'
sYr = int(s[2:])
if s.startswith('Z_') & sYr >= 15:
new_format = True
else:
new_format = False
print new_format
我已经评估了连词的两个部分,它们评估为 True,所以不确定我做错了什么。
【问题讨论】:
-
您是否有意使用位运算符
&而不是逻辑运算符and? -
写
new_format = s.startswith('Z_') and sYr >= 15可能更清楚
标签: python python-2.7 logic