user3699543
user3699543

Reputation: 51

How to test incorrect data input

Trying to create a while loop that is supposed to test whether or not the input is incorrect

while password_length >= MIN_PASSWORD_LENGTH or password_length <= MAX_PASSWORD_LENGTH

My while loop is supposed to test if the min length and max length in the password is meet, if not the user should have to input again but when I run my code and put a password that is less then min and more then the max the code still runs. I am confused as to what else am I supposed to add? Arent I already asking for password length is equal to or greater then Min length?

Upvotes: 0

Views: 74

Answers (1)

cullzie
cullzie

Reputation: 2755

When you use or it means if either side corresponds to True it will continue. For example:

while password_length >= MIN_PASSWORD_LENGTH or password_length <= MAX_PASSWORD_LENGTH:
# 6 >= 5 OR 6 <=10     $==>   True or True
# 1 >= 5 OR 1 <=10     $==>   False or True
# 11 >= 5 OR 11 <=10   $==>   True or False

So in every case there is a True value meaning your while loop will continue forever.

What you need to do is loop while one of your conditions is not met(i.e while one of them is False). In these cases you want to prompt again:

while not password_length >= MIN_PASSWORD_LENGTH or not password_length <= MAX_PASSWORD_LENGTH

As an improvement reduce it down to just this:

while not MAX_PASSWORD_LENGTH >= password_length >= MIN_PASSWORD_LENGTH

Upvotes: 1

Related Questions