如何在Python中的assert语句中使用小于和等于
当我运行以下命令时:
growthRates = [3, 4, 5, 0, 3]
for each in growthRates:
print each
assert growthRates >= 0, 'Growth Rate is not between 0 and 100'
assert growthRates <= 100, 'Growth Rate is not between 0 and 100'
我得到:
3
Traceback (most recent call last):
File "ps4.py", line 132, in <module>
testNestEggVariable()
File "ps4.py", line 126, in testNestEggVariable
savingsRecord = nestEggVariable(salary, save, growthRates)
File "ps4.py", line 106, in nestEggVariable
assert growthRates <= 100, 'Growth Rate is not between 0 and 100'
AssertionError: Growth Rate is not between 0 and 100
这是为什么?
When I run the following:
growthRates = [3, 4, 5, 0, 3]
for each in growthRates:
print each
assert growthRates >= 0, 'Growth Rate is not between 0 and 100'
assert growthRates <= 100, 'Growth Rate is not between 0 and 100'
I get:
3
Traceback (most recent call last):
File "ps4.py", line 132, in <module>
testNestEggVariable()
File "ps4.py", line 126, in testNestEggVariable
savingsRecord = nestEggVariable(salary, save, growthRates)
File "ps4.py", line 106, in nestEggVariable
assert growthRates <= 100, 'Growth Rate is not between 0 and 100'
AssertionError: Growth Rate is not between 0 and 100
Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要
:
Do:
not:
那么你的断言当然不会失败,但现在的增长率> 100,因为 GrowthRates 是列表,0 是整数,并且 'list'>'integer'。
Your asserts do not fail of course then, but now the growthRates > 100 because growthRates is list and 0 is integer and 'list'>'integer'.
断言(每个 >= 0)
不是
断言 (growthRates >= 0)
:-)assert (each >= 0)
not
assert (growthRates >= 0)
:-)您还可以使用:
You can also use:
测试每个而不是列表的增长率。
您还可以使用:
Test for each instead of the list growthRates.
You could also use: