-3
password = input(str("Please enter a password with a capital letter and a number: "))
for char in password:
    if password.islower() and "1234567890" not in password:
        print("Your password will need to have at least one number and at least one capitalized letter")
        password = input("Please enter another password: ")

**The error phrase will print if a password is entered without a number or capital, but if a capital is used in the input the error string doesn't run even though the input is still missing a number. Same for if the input has a number but not a capital letter. I want both a capital letter and a number to be required for the input as you can probably tell. Thanks.

edit: I don't want to know how to make a password requirement program. I specifically want to know why the "and not" is not working.**

  • 1
    Please use search before posting a new question. Please refer https://stackoverflow.com/questions/2990654/how-to-test-a-regex-password-in-python which is close to your scenario. – Sathish Guru V Jan 30 '20 at 09:38
  • stackoverflow.com/questions/2990654/… isn't what I'm looking for. The solutions in the comments of that post don't work for what I am wanting to know. Thanks.@Sathish Guru – Anderdoodle Jan 30 '20 at 09:50
  • 1
    You haven't actually implemented the logic your text describes. You want to reject a password that is lowercase **or** alphabetic, not only those that are lowercase *and* alphabetic. You want to reject a password that doesn't have *any* number in it, not one that doesn't have the consecutive numbers from 1-9 and 0 in it. – MisterMiyagi Jan 30 '20 at 09:55

2 Answers2

0

I specifically want to know why the "and not" is not working.

"1234567890" not in password

is negation of "1234567890" in password which for password being str is checking if "1234567890" is substring of password. Consider that:

print("123" in "123123123")  # True
print("123" in "1")  # False
print("123" in "321")  # False

To check if any character from one str is present in second str you might check if intersection is not empty - just turn second str into set, get interesection with first, and use bool function on result, thus getting True if at least one of characters of first str is present in second and False otherwise:

x = "1234567890"
y = "sometextandnumber0"
print(bool(set(y).intersection(x)))  # True
Daweo
  • 31,313
  • 3
  • 12
  • 25
  • Note that ``str.isdigit`` is simpler than creating sets of strings and their intersections. – MisterMiyagi Jan 30 '20 at 09:57
  • @MisterMiyagi: `str.isdigit` check if *all* characters are digits, so for `abc123` it gives `False`, when intersection solution check if *at least one* character is one from specified. – Daweo Jan 30 '20 at 10:00
  • You need to apply it per character, of course, i.e. ``any(map(str.isdigit, y))``. – MisterMiyagi Jan 30 '20 at 10:03
0

I just happened to write this yesterday. Season to taste :-)

import re
import getpass
while True:
    pwd = getpass.getpass('please enter a password: ')
    if len(pwd) >= 8 and re.search('[0-9]', pwd) and re.search('[A-Z]', pwd):
        if pwd != getpass.getpass('please reenter password: '):
            print('passwords do not match')
        else:
            break
    else:
        print('passwords must contain 8 characters and at least one uppercase letter and one digit')

print('approved pwd:', pwd)
Alias_Knagg
  • 886
  • 1
  • 7
  • 21