While loop status test | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

While loop status test

I'm struggling a bit with my status tests for while loops that are based on user inputs. All examples I've seen thus far are either addition or subtraction-based, so a simple ***while = True*** works easily enough. The questions I'm looking at are input() based, so I'm having a tough time creating a valid test to run through all potential inputs, then print the adjusted value out the end. So far I've tried: while input() !='' while True: while input() == True: Am I missing something simple here? Or am I a lesson or two before learning some easy control for a while loop?

10th Feb 2021, 6:00 PM
Rusty
Rusty - avatar
1 Answer
+ 3
# Hi! Try this: try: while True: s = input() if s == "q": break else: print(s) except(EOFError): print("=> Error: Quit program with enter 'q'!") else: print("'q' detected. Program quit!") # The ’try’ - ’except’ - ’else’ statement are there to catch up the EOFError (End of File Error) that is coming up in the SoloLearns environnent, when using input in the while statement with the True as condition, because of the special input process (with Submit). # Here I used the character ’q’ to quit the process, but you can try other conditions. # I set the ’while’ condition to be allways ’True’, and used instead ’break’ to jump out from the ’while’ process. The ’break’ breaks the ’while’ process, when the condtition in the ’if’ statement is true (here: when you input just ’q’ on a empy line and push enter (or Submit). Regards /Per B 🙂
10th Feb 2021, 6:12 PM
Per Bratthammar
Per Bratthammar - avatar