The following variable is defined: height = -20 What will happen when executing the following code: assert height > 0

Questions & AnswersCategory: PythonThe following variable is defined: height = -20 What will happen when executing the following code: assert height > 0
Lokesh Kumar Staff asked 2 years ago

The following variable is defined:

height = -20

What will happen when executing the following code:

assert height > 0

 
a. The assert statement checks that the value of the height variable is greater than 0. If the condition is false (and in this case it is – it returns False), no error will be raised and code execution continues on the next line (if any).
 
b. The assert statement checks if the value of the height variable is greater than 0. If the condition is true (and in this case it is – it returns True) then an AssertionErorr will be raised without any additional message.
 
c. The assert statement checks if the value of the height variable is greater than 0. If the condition is false (and in this case it is – it returns False) then an AssertionErorr will be raised with no additional message.

 

1 Answers
Lokesh Kumar Staff answered 2 years ago

c. The assert statement checks if the value of the height variable is greater than 0. If the condition is false (and in this case it is – it returns False) then an AssertionErorr will be raised with no additional message.
 
Explanation:

Assert statements are a convenient way to insert debugging assertions into a program:

assert_stmt ::= "assert" expression ["," expression]