Triple nesting while loops Python 3.6.5 is not indefinite [duplicate]


Triple nesting while loops Python 3.6.5 is not indefinite [duplicate]



This question already has an answer here:



I am trying to do triple nesting of while loops.
If you input a decimal number it returns error, then if you input number above 31 it returns error, but if you try again to input decimal number code stops. Need help making it indefinite loop no matter how many times, or what order, a user inputs incorrect format. Also need to verify that dates input are valid for number of days in given month?


import string

varD= input("Enter Date/Day:")

while varD.isdigit() or varD.isspace()
or varD.isdecimal or int(varD)>31
or int(varD)==26 or int(varD)<=0:
print ("Error: Enter Valid Number!")
varD= input("Enter Day:")

else:
print ("You have entered:", varD)



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





You have 3 different consecutive loops. Why not just use a single while True loop instead?
– Martijn Pieters
Jul 1 at 15:12


while True




1 Answer
1



Use an infinite loop and break only when all criteria are satisfied instead.


while True:
varD = input("Enter Day:")
if varD.isdigit() and not varD.isspace() and varD.isdecimal()
and int(varD) < 32 and int(varD) != 26 and int(varD) > 0:
break
print("Error: Enter Valid Number!")
print("You have entered: %s" % varD)



Also, your understanding of the term triple nesting is incorrect. Triple nesting means something like this:


while expression1:
while expression2:
while expression3:
do_something()





I would consider splitting that long conditional out into separate lines.
– Jonathon Reinhart
Jul 1 at 15:07





Indeed, I would too.
– blhsing
Jul 1 at 15:08





I actually meant several if statements, sorry.
– Jonathon Reinhart
Jul 1 at 15:09


if





I don't quite think that the way I write it now is any less readable than two if statements, but I can see where you're coming from.
– blhsing
Jul 1 at 15:11



if





Yes just realized I had to complete code on last print statement as yours was just placeholders, it works. Thanks
– Josh
Jul 2 at 3:27

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages