0

I am trying to make a code to validate a password. However it is not working, any ideas on how to fix this?

password = input("Please enter password: ")

length = False
digit = False
capital = False

length = len(password)

if length > 6:
    length = True
    #print("Length Good")

for count in password:
    if count.isdigit():
        digit = True
        #print ("Contains digit")

for count in password:
    if count.isupper():
        capital = True
        #print ("Contains a capital")

if length != True and digit != True and capital != True:
    print("Password is good")
else:
    print("Bad password")

any ideas on how to fix this, thanks

6
  • length cannot be the length of your password and also a true/false value. Use two variables. Commented Jan 24, 2021 at 22:08
  • (1) You can't use the same variable "length" for two purposes at the same time. (2) A password like "zgjf" is accepted as good. Commented Jan 24, 2021 at 22:08
  • 1
    Also, your code requires a "good" password to not contain a capital letter and not contain a digit. That seems unlikely to be what you intended. Commented Jan 24, 2021 at 22:09
  • @khelwood I have changed the code but it still doesn't seem to work? thanks Commented Jan 24, 2021 at 22:11
  • @MichaelButscher I have changed the code but it still doesn't seem to work? thanks Commented Jan 24, 2021 at 22:11

4 Answers 4

3

I ran it and entered "test" and it said "Password is good". I suspect you meant to have:

password = input("Please enter password: ")

length = False
digit = False
capital = False

length = len(password)

if length > 6:
    length = True
    #print("Length Good")

for count in password:
    if count.isdigit():
        digit = True
        #print ("Contains digit")

for count in password:
    if count.isupper():
        capital = True
        #print ("Contains a capital")

if length == True and digit == True and capital == True:
    print("Password is good")
else:
    print("Bad password")

because otherwise you are enforcing bad passwords.

Sign up to request clarification or add additional context in comments.

2 Comments

Cool, great @Katie010203 Can you mark this as the correct answer so I can get the points?
I will, it says I have to wait 1min thou x
0

In your code, you are assigning the length of your password to the variable length, and then promptly overwriting the value in length with False:

length = len(password)
length = False

Also, you be checking if length, digit, and capital are True, as checking them for False will give you opposite results unless you flip the statement

You should instead move len(password) to the check itself, like so:

password = input("Please enter password: ")

length = False
digit = False
capital = False

if len(password) > 6:
    length = True
    # print("Length Good")

for count in password:
    if count.isdigit():
         digit = True
         # print ("Contains digit")
    elif count.isupper():
         capital = True
         # print ("Contains a capital")

if length == True and digit == True and capital == True:
    print("Password is good")
else:
    print("Bad password")

Comments

0

You are setting the

length = len(password)

and after

length = False

so Python first set length like an int, and in the second instruction Python cancel the value of variable and set the length variable like a boolean(False) and the condition length>6 failed.

Comments

0

Used Bitwise OR operator to validate the password.

Time complexity is O(n).

def validate(password):
    sum = 0
    if len(password) < 7:
        return False
    for p in password:
        if p.isdigit():
            sum |= 1    #2^0
        elif p.isupper():
            sum |= 2    #2^1
        # elif not p.isalnum():
        #     sum |= 4    #2^2

    if sum == 3:    # 2^n - 1 where n is the no. of conditions inside for loop
        return True
    else:
        return False

password = 'voteUp1IfUsefulAnswer'
result = validate(password)
if result:
    print("Password is good")
else:
    print("Bad password")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.