assert syntax python
assert <condition>,<error message>
#The assert condition must always be True, else it will stop execution and return the error message in the second argument
assert 1==2 , "Not True" #returns 'Not True' as Assertion Error.
assert syntax python
assert <condition>,<error message>
#The assert condition must always be True, else it will stop execution and return the error message in the second argument
assert 1==2 , "Not True" #returns 'Not True' as Assertion Error.
what is assert
In simple words, if the assert condition is true then the
program control will execute the next test step but if the condition is
false, the execution will stop and further test step will not be executed.
assert keyword in python
The assert keyword is used when debugging code.
The assert keyword lets you test if a condition in your code returns
True, if not, the program will raise an AssertionError.
You can write a message to be written if the code returns False, check
the example below.
x = "hello"
#if condition returns False, AssertionError is raised:
assert x == "goodbye", "x should be 'hello'"
python assert
def input_age(age):
try:
assert int(age) > 18
except ValueError:
return 'ValueError: Cannot convert into int'
else:
return 'Age is saved successfully'
print(input_age('23')) # This will print
print(input_age(25)) # This will print
print(input_age('nothing')) # This will raise ValueError which is handled
print(input_age('18')) # This will raise AssertionError, program collapses
print(input_age(43)) # This won't print
python assert
assert False, "Oh no! This assertion failed!"
how to use assert in python
assert type(num) is int,"num must be an integer"
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us