+ 3

About asserts

try: print(1) assert 2+2==5 except AssertionError: print(3) except: print(4) If I will run this code, the result is 3. What I think is that assert must take false and must not run the except. Or it is running except because assert took false?

12th Jul 2017, 9:41 AM
Yusuf
Yusuf - avatar
10 ответов
+ 8
Why is everyone smarter than me :(
12th Aug 2020, 11:32 AM
Amani TC
Amani TC - avatar
+ 7
If the assertion returns False it runs the AssertionError clause if one exists, if you only had the general except clause then it would print 4. If the assertion returns True, no exceptions will be called (:
12th Jul 2017, 10:38 AM
Maya
Maya - avatar
+ 3
Answer is 3
28th Feb 2021, 11:12 AM
Sasanka Vikum Vitharana
Sasanka Vikum Vitharana - avatar
+ 1
Quantr Post your code, but it's probably a syntax error as stated.
25th Apr 2020, 8:44 AM
Yusuf
Yusuf - avatar
+ 1
3
22nd Nov 2021, 10:04 AM
Ozodbek
+ 1
3
1st Feb 2022, 9:46 AM
KESHAV KONDABALA
KESHAV KONDABALA - avatar
+ 1
3
28th Apr 2022, 5:29 AM
CHERUKUMALLI VIGNESH KUMAR
0
3
17th Jun 2022, 10:48 PM
Gift Mejuru
- 1
when i tryed this code it said syntax error could be because i changed the print variable?
25th Apr 2020, 5:55 AM
Quantr
Quantr - avatar
- 2
try: print(1) assert 2+2==5 == > This is False , of assert occurs except AssertionError: ==> but we are handling the asset exception, so it will come here and print 3, if except AssertionError: was not there , then the program would have terminated after printing 1 print(3) == > Print 3 except: == > This will never be called print(4)
13th Jul 2017, 5:35 AM
J Prak
J Prak - avatar