Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use less than and equal to in an assert statement in python

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?

like image 325
Noah Clark Avatar asked Nov 27 '22 23:11

Noah Clark


2 Answers

Do:

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

not:

assert growthRates >= 0, 'Growth Rate is not between 0 and 100'
like image 144
Katriel Avatar answered Dec 15 '22 12:12

Katriel


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'.

like image 28
Tony Veijalainen Avatar answered Dec 15 '22 14:12

Tony Veijalainen