While loops in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

While loops in python

How do you exit a while loop in python on solo when there is no input.

3rd May 2020, 10:41 AM
Wonderwoman
Wonderwoman - avatar
13 Answers
+ 11
Wonderwoman i = 0 while 1==1: print(i) i = i + 1 if i >= 5: print("Breaking") break print("Finished")
3rd May 2020, 10:59 AM
Gaurav Dixit🇮🇳
Gaurav Dixit🇮🇳 - avatar
+ 4
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 When statement is not true then loop is automatically break dear pls read code carefully
3rd May 2020, 10:50 AM
Gaurav Dixit🇮🇳
Gaurav Dixit🇮🇳 - avatar
+ 4
Wonderwoman Use meaning full condition If condition matches loop is going on and condition is not match the use break statement to break loop
3rd May 2020, 10:56 AM
Gaurav Dixit🇮🇳
Gaurav Dixit🇮🇳 - avatar
+ 2
i = 1 while i <=5: print(i) i = i + 1 print("Finished!")
3rd May 2020, 10:46 AM
Gaurav Dixit🇮🇳
Gaurav Dixit🇮🇳 - avatar
3rd May 2020, 10:55 AM
Wonderwoman
Wonderwoman - avatar
+ 1
What if my loop depends on the input and if there's no input... How would i write that condition? I tried using an empty string but it doesn't always work
3rd May 2020, 10:52 AM
Wonderwoman
Wonderwoman - avatar
+ 1
So for no input the condition will be? If input = '': Break But this doesn't always work
3rd May 2020, 10:58 AM
Wonderwoman
Wonderwoman - avatar
+ 1
Yes but this one is basic, I was looking for an explanation on no input condition
3rd May 2020, 11:02 AM
Wonderwoman
Wonderwoman - avatar
+ 1
I think this is what you mean If there is no user input how should the code or program respond Thats why you have "break" But preferably what you do is use the the "while" loop in combination with "elif" and "if" or "else" This returns a different output when your 'if' code is not inputed
3rd May 2020, 1:05 PM
Tega Somuvie
Tega Somuvie - avatar
+ 1
Yes
3rd May 2020, 1:22 PM
Wonderwoman
Wonderwoman - avatar
+ 1
add a break at the end if the loop
4th May 2020, 11:48 PM
Kevin Orukele
Kevin Orukele - avatar
0
i = 0 while 1==1: print(i) i = i + 1 if i >= 5: print("Breaking") break print("Finished") In this code 2nd line, what is the purpose for writing while 1==1: What does it do?
1st Sep 2020, 6:33 AM
Daniel Chettiar
Daniel Chettiar - avatar
0
Which statement ends the current iteration and continues with the next one?
28th Oct 2020, 4:16 AM
HEENAL VERMA
HEENAL VERMA - avatar