Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3
Assertions are used to detect programming bugs. You wouldn't use them to detect errors in user entered data. They generate a program crashing exception so you can see the call frame and fix the code. Say you are converting money from dollars to pounds. You expect the exchange rate to be a positive number above 0 and less than 4. You could assert the number is in that range catching an error in your code reading it from the Internet or file. I'm guessing you are talking Python so using it's syntax with this, but the concept is identical for all languages supporting it: assert exchange > 0.0 and exchange < 4.0, "invalid exchange rate" If the condition is false, the program halts with the message and call frame.
18th Jan 2019, 1:02 PM
John Wells
John Wells - avatar