Solving password's task in python

I have tried to solve this task but can’t find what is wrong with my code.

I didn’t succeed. I would like to find where exactly I am wrong and why. Also, I know it is possible to solve in another way but want to understand what is wrong with my code

The task is about:
ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.

If the function is passed a valid PIN string, return true, else return false.

My code:

def validate_pin(pin):
    if len(pin) != 4 or len(pin) != 6:
        return False
    else:
        return pin.isdigit()

>Solution :

I think your issue is with the boolean logic including negatives as well as an or.

You want "the pin must be 4 digits or 6 digits. if it is neither, it is bad". But notice in the way I phrased it in normal language, the logic each time is equals, rather than not-equals, and using the "or".

You have your comparisons here as not-equals, which is fine, but then you need "and", not "or". You should have:

def validate_pin(pin):
    if len(pin) != 4 and len(pin) != 6:
        return False
    else:
        return pin.isdigit()

Put more generally, given the statement P or Q (both true), the negation of this is ~P and ~Q (the little ~ meaning ‘not’).

I’m pretty sure this is De Morgan’s Law

edit: add everything below here

To make this easier to parse, the best course of action is probably to just make the boolean logic more intuitive but checking that it is what you want (rather than checking that it isn’t).

def validate_pin(pin):
    if len(pin) == 4 or len(pin) == 6:
        return pin.isdigit()
    else:
        return False

Leave a Reply