Student Student - 5 months ago 6
Python Question

How would I check how many numbers are in a string in python?(Password strength checker)

I am making a password strength checker as a school project, but I am struggling to find a way to check how many integers are in the password, as such I am checking the password on three things, but I just cannot figure out how to check for integers. If the solution you guys could help me with could include the re library that would be even better. Thanks!

Here is my problematic code so far.

import re

i_pwdStrength = i_pwdLength + i_pwdChar + i_pwdInt
i_pwdLength = 0
i_pwdChar = 0
i_pwdInt = 0
i_attempts = 0
numbAttemps = int(input("Enter the amount of passwords you would like to check \n(MUST BE A NUMBER)"))
pwd = input("Enter Password")


while numbAttempts < i_attempts
pwd = pwd.lower()

i_pwdLength = pwd.length
if i_pwdLength <= 3:
i_pwdLength = 1
elif i_pwdLength > 3 && <= 6:
i_pwdLength = 2
elif i_pwdLength > 6 && <= 9:
i_pwdLength = 3
else i_pwdLength > 9:
i_pwdLength = 4

i_pwdChar = #This is a very long way of writing this code as I could not find a way to compress these lines. I may have used the 'or' command but I could not
if re.search(r'[x]',pwd): # work out how to integrate it and i thought that this code looked neater
i_pwdChar = 1
elif re.search(r'[w]'pwd):
i_pwdChar = 1
elif re.search(r'[y]'pwd):
i_pwdChar = 1
elif re.search(r'[z]'pwd):
i_pwdChar = 1
elif re.search(r'[x]',pwd) and re.search(r'[w]',pwd):
i_pwdChar = 2
elif re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
i_pwdChar = 2
elif re.search(r'[x]',pwd) and re.search(r'[z]',pwd):
i_pwdChar = 2
elif re.search(r'[w]',pwd) and re.search(r'[y]',pwd):
i_pwdChar = 2
elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd):
i_pwdChar = 2
elif re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
i_pwdChar = 2
elif re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
i_pwdChar = 3
elif re.search(r'[z]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
i_pwdChar = 3
elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd) and re.search(r'[y]',pwd):
i_pwdChar = 3
else re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd) and re.search(r'[z]',pwd)
i_pwdChar = 4


Due to all the help I have received I have finished the code.

import re

i_numbAttempts = int(input("How many passwords do you want to try?\nMUST BE A NUMBER!")) # Determines how many times the while loop further down
attempts = 0 # sets the variable attempts to 0 to ensure that the while loop works # repeats itself

while i_numbAttempts > attempts: # while the passwords entered is more than attempts:
pwd = input("Enter Password: ") # user enters password to check
pwd = pwd.lower() # sets the password to lowercase (QjwtwWyeRvgTRDU would become qjwtwwyervgtrdu)

if len(pwd) == 0: # if the user does not enter a password, it outputs: You must ypre something!
print("You must type something!")
i_pwdLength = 0
elif len(pwd) >= 1 and len(pwd) <= 3: # if the length is a certain length,
i_pwdLength = 1 # it will assign a corresponding value to the i_pwdLength variable
elif len(pwd) > 3 and len(pwd) <= 6:
i_pwdLength = 2
elif len(pwd) > 6 and len(pwd) <= 9:
i_pwdLength = 3
elif len(pwd) > 9:
i_pwdLength = 4

if re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
i_pwdChar = 4
elif re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
i_pwdChar = 3
elif re.search(r'[z]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
i_pwdChar = 3
elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd) and re.search(r'[y]',pwd):
i_pwdChar = 3
elif re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
i_pwdChar = 4
elif re.search(r'[x]',pwd) and re.search(r'[w]',pwd): # This section checks the password for any
i_pwdChar = 2
elif re.search(r'[x]',pwd) and re.search(r'[y]',pwd): # Combination of the letters w,x,y and z.
i_pwdChar = 2
elif re.search(r'[x]',pwd) and re.search(r'[z]',pwd): # I could not find a way to compress these
i_pwdChar = 2
elif re.search(r'[w]',pwd) and re.search(r'[y]',pwd): # Lines so i left them as is as i thought
i_pwdChar = 2
elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd): # That it looked neater
i_pwdChar = 2
elif re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
i_pwdChar = 2
elif re.search(r'[x]',pwd):
i_pwdChar = 1
elif re.search(r'[w]',pwd):
i_pwdChar = 1
elif re.search(r'[y]',pwd):
i_pwdChar = 1
elif re.search(r'[z]',pwd):
i_pwdChar = 1
else:
i_pwdChar = 0

if (len([l for l in pwd if l.isdigit()])) == 0: # This code checks how many integers are included
i_pwdInt = 0
elif (len([l for l in pwd if l.isdigit()])) >= 1 and (len([l for l in pwd if l.isdigit()])) < 3: # In the password, and assigns a number to the
i_pwdInt = 1
elif (len([l for l in pwd if l.isdigit()])) >= 3 and (len([l for l in pwd if l.isdigit()])) < 5: # Variable i_pwdInt accordingly
i_pwdInt = 2
elif (len([l for l in pwd if l.isdigit()])) >=5 and (len([l for l in pwd if l.isdigit()])) <7:
i_pwdInt = 3
elif (len([l for l in pwd if l.isdigit()])) >=7:
i_pwdInt = 4

i_pwdStrength = i_pwdLength + i_pwdChar + i_pwdInt # this determines the passwords overall strength

if i_pwdStrength > 0 and i_pwdStrength <= 4 :
print("This password is bad")
elif i_pwdStrength > 4 and i_pwdStrength <= 8:
print("This password is OK")
elif i_pwdStrength > 8 and i_pwdStrength <= 11:
print("this password is good")
elif i_pwdStrength == 12:
print("this password is the best!!")

attempts = attempts + 1

Answer

I think you should use isdigit method. The following command will return you the number of digits in your password.

len([l for l in pwd if l.isdigit()])
Comments