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?
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 đ