Pregunta

Cuando ejecuto el siguiente:

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'

Puedo obtener:

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

¿Por qué es eso?

¿Fue útil?

Solución

Do:

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

No:

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

Otros consejos

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

Su afirma no fallan, por supuesto, pero ahora los growthRates> 100 porque es growthRates lista y 0 es entero y 'lista'> 'entero'.

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

También puede usar:

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

ensayo para cada lugar de las growthRates lista.

También es posible usar:

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'
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top