+ 2
Assertion
What's the difference between assertion & except in python?
1 Odpowiedź
+ 1
syntax :
assert condition
eg :
assert 5>2
## all fine no output
assert 5<2
## Assertion Error
so...
assert True is ok...
assert False is Assertion error...
except is not alone..
its try-except-(often finally) combo..
suppose...
n is a string...
u wanna do smthing if n is a int..
so u cant check it with if...
## actually u can but its bit annoying and not ideal...
so u do....this
try :
n=int(n)
except :
print("sry n needs to be an integer")
if n is a string....int(n) will give Value error..
but within try statement...
and it will jump to except statement and prints that thing.
try this code...
n="solo"
n=int(n)
## see which error compiler gets##
(valueerror)