What does this mean? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What does this mean?

What is the sum of the numbers printed by this code? try: print(1) print(1 + "1" == 2) print(2) except TypeError: print(3) else: print(4) Why is the answer 4 to this code?

2nd Jan 2020, 10:31 PM
Tom
10 Answers
+ 8
The answer IS 4... :) "What is the SUM of the numbers printed by this code?" try: print(1) # <-- output print(1 + "1" == 2) # raises TypeError exc print(2) except TypeError: print(3) # <-- output else: print(4)
2nd Jan 2020, 10:53 PM
unChabon
unChabon - avatar
+ 5
correct answer is 4.
22nd Mar 2021, 6:27 AM
Aman
Aman - avatar
+ 4
Ha - you're right, I just looked at the code and somehow missed the question. Sorry, Tom!
2nd Jan 2020, 10:54 PM
HonFu
HonFu - avatar
+ 4
Relax :) jeje
2nd Jan 2020, 10:55 PM
unChabon
unChabon - avatar
+ 3
The answer is not 4. Please run your code, before you ask a question, so that you're asking the right question. If an error occurs in try, the code will jump to except. Only if no error occurs, else will eventually be executed. Line 1 is okay, so 1 is printed. Line 2 tries to add an int to a str, although they are different types. This raises a TypeError, so we jump into except and print 3. Then it ends.
2nd Jan 2020, 10:37 PM
HonFu
HonFu - avatar
+ 2
The number 1 will be printed. Since there is error in the next line, 3 will be printed. 2 won't print because the execution of code has stopped in the previous line. So, it's 1+3=4
4th Apr 2021, 3:08 PM
Steffi George
Steffi George - avatar
0
try: print(1) print(1 + "1" == 2) print(2) except TypeError: print(3) else: print(4) 4 # output :- 1 3
8th Jan 2021, 11:40 AM
Diptiranjan Malik
0
Well, @HonFu It seems that the correct answer is 4 because they may be a problem or glitch with your computer. Don't give people the wrong answer.
13th Jan 2022, 8:30 PM
Thuwayba Ahmed
Thuwayba Ahmed - avatar
0
***************************answer is 4 guys ***************
16th Aug 2022, 6:26 AM
Iman Jafari
Iman Jafari - avatar
- 5
9 by guessss
8th Jun 2020, 9:22 AM
gashaw desalegn
gashaw desalegn - avatar