如何在Python中的assert语句中使用小于和等于

发布于 2024-09-15 23:42:13 字数 704 浏览 7 评论 0原文

当我运行以下命令时:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

墨小沫ゞ 2024-09-22 23:42:13

不要

assert each >= 0, 'Growth Rate is not between 0 and 100'

assert growthRates >= 0, 'Growth Rate is not between 0 and 100'

Do:

assert each >= 0, 'Growth Rate is not between 0 and 100'

not:

assert growthRates >= 0, 'Growth Rate is not between 0 and 100'
完美的未来在梦里 2024-09-22 23:42:13
assert 0 <= each <= 100, 'Growth Rate %i is not between 0 and 100.' % each

那么你的断言当然不会失败,但现在的增长率> 100,因为 GrowthRates 是列表,0 是整数,并且 'list'>'integer'。

assert 0 <= each <= 100, 'Growth Rate %i is not between 0 and 100.' % each

Your asserts do not fail of course then, but now the growthRates > 100 because growthRates is list and 0 is integer and 'list'>'integer'.

写下不归期 2024-09-22 23:42:13

断言(每个 >= 0)
不是 断言 (growthRates >= 0) :-)

assert (each >= 0)
not assert (growthRates >= 0) :-)

绻影浮沉 2024-09-22 23:42:13

您还可以使用:

growthRates = [0, 10, 100, -1]
assert all(0<=each<=100 for each in growthRates), 'growthRate is not between 0 and 100'

Traceback (most recent call last):
File "any.py", line 2, in <module>
assert all([0<=each<=100 for each in growthRates]), 'growthRate is not between 0 and 100'
AssertionError: growthRate is not between 0 and 100

You can also use:

growthRates = [0, 10, 100, -1]
assert all(0<=each<=100 for each in growthRates), 'growthRate is not between 0 and 100'

Traceback (most recent call last):
File "any.py", line 2, in <module>
assert all([0<=each<=100 for each in growthRates]), 'growthRate is not between 0 and 100'
AssertionError: growthRate is not between 0 and 100
眼趣 2024-09-22 23:42:13

测试每个而不是列表的增长率。

您还可以使用:

growthRates = [3, 4, 5, 0, 3]
testRange = range(0,100)
for each in growthRates:
    print each
    assert each in testRange, 'Growth Rate is not between 0 and 100'

Test for each instead of the list growthRates.

You could also use:

growthRates = [3, 4, 5, 0, 3]
testRange = range(0,100)
for each in growthRates:
    print each
    assert each in testRange, 'Growth Rate is not between 0 and 100'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文