简体   繁体   中英

Python3 instruction assert with condition

I work in python 3. I calculate the average and I would like to use an assert instruction.

moyenne = somme/nombre_notes   if somme==50 and nombre_notes==4 :   assert moyenne==12.5 ,"Erreur de calcul" 

This solution is operational but how write my assert in 1 line and begin by assert ....? Thank

You can build the conditional checks into the assertion like this:

somme=50
nombre_notes=4
moyenne = somme/nombre_notes
assert somme == 50 and nombre_notes == 4 and moyenne == 12.5, "Erreur de calcul"

最后,我找到了一个解决方案:进行单元测试,如下所示:

assert division(50, 4) == 12.5, "erreur de calcul" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM