-2

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)
Josh
  • 3
  • 3

1 Answers1

2

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()
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • I would consider splitting that long conditional out into separate lines. – Jonathon Reinhart Jul 01 '18 at 15:07
  • Indeed, I would too. – blhsing Jul 01 '18 at 15:08
  • I actually meant several `if` statements, sorry. – Jonathon Reinhart Jul 01 '18 at 15:09
  • 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 01 '18 at 15:11
  • Tried your infinite loop and when I did input correct number it still said invalid number. Also i have previously attempted the correct way to nest but was still missing something, the code crashed immediately. My first example was just to show how far I came before encountering issues or errors. – Josh Jul 02 '18 at 01:15
  • @Josh Did not notice that `varD.isspace()` needed to have a `not` before it. I just edited my answer and the logic should be correct now. Please try again. – blhsing Jul 02 '18 at 01:59
  • @Josh Is it still not working for you? It works for me. What kind of behavior or error are you getting? – blhsing Jul 02 '18 at 03:00
  • I get “Error enter valid number!” When I try the following one at a time: “76, .6, 26,0, space, empty, a letter, and what should be a valid input 2”. Of course currently I am on my mobile Python3IDE but I tried it earlier on laptop running python 3.6.5. I am a noob so I am probably missing something, but my goal is to write my program as I am learning python so I am trying to understand all lines of code not just copy/paste. I updated original post with adjustments from responses here and further reading of duplicate answer, but don’t know if it is an improvement or not. Thanks for your help. – Josh Jul 02 '18 at 03:18
  • @Josh Did you use my updated code or the original one? As I mentioned above, I've added `not` before `varD.isspace()` and that should make the code work. Make sure you have the updated code. – blhsing Jul 02 '18 at 03:23
  • 1
    Yes just realized I had to complete code on last print statement as yours was just placeholders, it works. Thanks – Josh Jul 02 '18 at 03:27
  • @Josh You're welcome. Can you mark this answer as accepted if you think it's correct? – blhsing Jul 02 '18 at 03:32
  • Just to make sure I understand: within the while statement all things have to be true, but if you input a decimal it would be true, but because.isdecimal is within the if statement and true it breaks to the error message. But I don’t understand if “not .decimal” is used and you input say 5, why would that not be true for all conditions? – Josh Jul 02 '18 at 04:14
  • @Josh No, the `break` statement breaks the `while` loop, not the `if` statement, so when it breaks it will go outside the `while` loop and print the "You have entered:" message. – blhsing Jul 02 '18 at 04:28
  • @blhsing ok I get that, but why ‘not .decimal’ ? Its definition states: “Returns true if a unicode string contains only a decimal character and false otherwise. “ eg. ‘5.0’ is true and ‘5’ is false. Is that right? – Josh Jul 02 '18 at 10:51
  • @Josh `isdecimal` does not include `.` at all, but is just like `isdigit`, except it does not test for unicode characters that also look like digits. See: https://stackoverflow.com/a/42250941/6890912 – blhsing Jul 02 '18 at 13:01
  • @blhsing Got it. Thanks for the reference. – Josh Jul 02 '18 at 21:14