+ 2

Why can't this code break? It's infinite

Using while loop and break https://code.sololearn.com/cRg9BoNL6pgM/?ref=app

2nd Jan 2019, 10:02 AM
David Arumba
David Arumba - avatar
7 Respostas
+ 6
You defined a string x with value of x. So when you use len(x) function it will always return 1 in answer (because string length of x is 1). To perform such type of task, you can use another variable. Like this : x="x" count = 0 while True: print(x) count += 1 if count>=10: break here count variable is intialized with value of 0. when the loop runs again and again, it will increase value 1 by one and when it will be >= 10, loop will be break.
2nd Jan 2019, 10:13 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 4
Hey there, inside your loop you're checking for the length of your x-variable to be greater/equal to 10. Since you never change to the value of x, it always stays "x"! Thats why you never break out of the loop. The len()-function probably just checks the length of the x-variable, which always stays at 1 in your code ;-).
2nd Jan 2019, 10:11 AM
Code Artist
Code Artist - avatar
+ 4
Hi. Try printing out the length of x, you'll see it's 1, therefore the condition in if statement will never be fulfilled and the code will never break. I'm not really sure what are you trying to achieve? Can you please explain again?
2nd Jan 2019, 10:16 AM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 3
Hello, x = "x" Len(x) mean length of x which is always 1 To break the loop you should add new variable to count like so: While i<10: print(x) i += 1 or for your code: i = 0 while True: print(x) i += 1 if i>10: break
2nd Jan 2019, 10:16 AM
just trying to think
just trying to think - avatar
+ 2
What is your point exactly? What is this code supposed to do?
2nd Jan 2019, 2:44 PM
Nboumakis
2nd Jan 2019, 8:38 PM
David Arumba
David Arumba - avatar
0
If condition won't executive ... Length of x remains same
10th Jan 2019, 8:18 AM
VAMSI KRISH
VAMSI KRISH - avatar