+ 1

Explain this question please

What is the largest number this code prints? for i in range(10): if i > 5: print(i) break else: print("7") #python

23rd May 2020, 8:36 AM
Mausam Kumar Mishra
20 Answers
+ 5
Let's simplify code :--- -- in this loop i is being incremented by 1 when i = 0 ( i > 5 is False ) i = 1 ( i > 5 is False ) i = 2 ( i > 5 is False ) i = 3 ( i > 5 is False ) i = 4 ( i > 5 is False ) i = 5 ( i > 5 is False ) i = 6 ( i > 5 is True ) when condition is True -- code executes -- prints 6 -- loop breaks this is why largest number this code prints is 6 Hope this helps !
23rd May 2020, 9:31 AM
Nachiketa
Nachiketa - avatar
+ 3
Hi, what is the output? after: which of the printed numbers is the highest?
23rd May 2020, 8:39 AM
Oma Falk
Oma Falk - avatar
+ 3
Priyanshu Gupta First of all else condition is executed when for loop is not terminated by break or any condition in for loop is not true & second thing is, in this loop when condition becomes true we prints i and break statement terminate the loop so when condition is true and loop is terminated by break statement. Then else condition will not be executed.
25th May 2020, 4:46 AM
Nachiketa
Nachiketa - avatar
+ 2
💞𝐒𝐨𝐦𝐲𝐚💞 what is output if i is 1🧐
23rd May 2020, 8:51 AM
Oma Falk
Oma Falk - avatar
23rd May 2020, 9:03 AM
Oma Falk
Oma Falk - avatar
+ 2
Code Crasher Yupp ! My Best wishes with you for your exam
23rd May 2020, 1:59 PM
Nachiketa
Nachiketa - avatar
+ 1
💞𝐒𝐨𝐦𝐲𝐚💞 the indent of else might be missing accidently or ment as little trap.
23rd May 2020, 9:01 AM
Oma Falk
Oma Falk - avatar
+ 1
Code Crasher Some learning stuff should only be experienced by own. Thank you for your concern !
23rd May 2020, 12:03 PM
Nachiketa
Nachiketa - avatar
+ 1
The largest number is six since "7" is not a number but a string. To understand why it is six: The for loop will terminate immediately it printed out the first int > 5 which is six Although it printed 7 five times d answer is not seven since the seven printed out is a string not a number
24th May 2020, 8:24 PM
Ttech%
Ttech% - avatar
+ 1
The output will be 6
24th May 2020, 8:25 PM
Haidar Mazen Hasan
Haidar Mazen Hasan - avatar
+ 1
Output will be 7 7 7 7 7 7 6 , beacause when if block comes true , loop breaks and , iteration stops. Before this, else block comes true , and prints 7
25th May 2020, 3:02 AM
Priyanshu Gupta(प्रियांशु गुप्ता)
Priyanshu Gupta(प्रियांशु गुप्ता) - avatar
+ 1
Output -> 7 7 7 7 7 7 6 Largest number -> 7
25th May 2020, 7:11 AM
Harshit Jain
Harshit Jain - avatar