lundi 15 juin 2020

Is there an operator or more elegant way of writing an if-statement?

I am still in the beginning stages of learning and obviously trying to shorten instances like this, where I have many if conditionals to go through. Just wondering how someone more experienced tackles this? I have tried with 'or' and 'and' operators, and I try to nest my statements where I see I can, but generally it still feels quite clumsy. This bit of code was part of a date-validator using regex for How To Automate The Boring Stuff, I don't have a else statement as there is more conditionals in the full code. Is there a better way or am I over complicating it?

    if month == 4 and day == 31:
            print("Invalid date found in text")
            return False
    elif month == 6 and day == 31:
            print("Invalid date found in text")
            return False
    elif month == 9 and day == 31:
            print("Invalid date found in text")
            return False
    elif month == 11 and day == 31:
            print("Invalid date found in text")
            return False

Aucun commentaire:

Enregistrer un commentaire