Checking password input for integers, capitals and lowercase? - python

I am doing some work for my computing class at school and we need to ask for input of a password. The input needs to be between 6 and 12 characters, and contain uppercase, lowercase and numbers in it. I have this so far :
import sys
import os
def checkPass():
passLoop = True
while passLoop:
print("Welcome user!")
x = len(input("Please enter your password, between 6 and 12 characters. "))
if x < 6:
print("Your password is too short")
r = input("Please press any key to restart the program")
elif x > 12:
print("Your password is too long")
r = input("Please press any key to restart the program")
else:
print("Thank you for entering your password.")
print("Your password is strong. Thank you for entering!")
passLoop = False
checkPass()
I need your help checking for uppercase, lowercase and integers. I'm only young so please don't be too harsh!

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d).{6,12}$
You can try this.This employs re.
You can use it like
import re
if re.match(r"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d).{6,12}$",pass,re.M):
print "valid"
else:
print "invalid"
See demo.
http://regex101.com/r/dZ1vT6/28

let's assume you have the password stored in a variable named passwd.
Then we can take your literal requirements and write checks for them:
The input needs to be between 6 and 12 characters,
if not 6 <= len(passwd) <= 12: ...
and contain uppercase,
if not any(c.isupper() for c in passwd): ...
lowercase
if not any(c.islower() for c in passwd): ...
and numbers in it.
if not any(c.isdigit() for c in passwd): ...
On a side note: you really should not limit the length of passwords.

the any function in combination with a map can be helpful. The map function iterates over all characters of the given string and test via the given lambda function if the character is an uppercase. The is consumed by the any function which returns true on the first true from the map function
>>> any(map(lambda a:a.isupper(),"aba"))
False
>>> any(map(lambda a:a.isupper(),"aBa"))
True
you can wrap that into a separate function like
>>> def test(passwd,func):
return any(map(func,passwd))
>>> test("aBa",str.isupper)
True
>>> test("ab0",str.isdigit)
True
>>> test("aBa",str.islower)
True

Related

Why doesn't if any(var0) == any(var1) work?

I'm very new to programming, and more new to Python, and I'm having a hard time figuring this out. I'm trying to make a simple text hangman game, and I'm trying to validate player1's input. Here's what I have so far.
a = 0
int = ["1","2","3","4","5","6","7","8","9","0"]
while a == 0:
print("Please choose any word by typing it here:")
d = input()
d = list(d)
print(d)
if any(int) == any(d):
print("Invalid input. Please choose a non-number with no spaces or special characters.")
I can't figure why, whether I include a number in my response or not, it always meets my any(int) == any(d) condition. I know I must just be misunderstanding how any() works.
You are right: the issue is with how you use the any() function. any() returns either True or False depending on whether or not any of the elements in its iterable argument are True, in other words if they exist and are not False or an empty list or empty string or 0. This will always evaluate as True for int because int does contain an element that is True and the same will happen for d unless you do not give any input when the input() function prompts you (you hit return without typing anything). What your conditional is basically asking is the following:
if True==True
To fix this, just change your code to the following:
a = 0
int = ["1","2","3","4","5","6","7","8","9","0"]
while a == 0:
print("Please choose any word by typing it here:")
d = input()
print(d)
for letter in d:
if letter in int:
print("Invalid input. Please choose a non-number with no spaces or special characters.")
break
The easiest solution, however, does not involve the int list at all. There is actually a special method in python that can gauge whether or not a string has a number (or special character) in it, and this method, .isalpha() can be used to streamline this process even further. Here's how to implement this solution:
while True:
d = input("Please choose any word by typing it here: \n")
print(d)
if not d.isalpha():
print("Invalid input. Please choose a non-number with no spaces or special characters.")
Try this:
a = 0
ints = ["1","2","3","4","5","6","7","8","9","0"]
while a == 0:
print("Please choose any word by typing it here:")
word = input()
word_list = list(word)
if any(letter in ints for letter in word_list):
print("Invalid input. Please choose a non-number with no spaces or special characters.")

Check Python String Formatting

I can accept user's input in two formats:
123
123,234,6028
I need to write a function/a few lines of code that check whether the input follows the correct format.
The rules:
If a single number input, then it should be just a number between 1 and 5 digits long
If more then one number, then it should be comma-separated values, but without spaces and letters and no comma/period/letter allowed after the last number.
The function just checks if formatting correct, else it prints Incorrect formatting, try entering again.
I would assume I would need to use re module.
Thanks in advance
You can use a simple regex:
import re
validate = re.compile('\d{1,5}(?:,\d{1,5})*')
validate.fullmatch('123') # valid
validate.fullmatch('123,456,789') # valid
validate.fullmatch('1234567') # invalid
Use in a test:
if validate.fullmatch(your_string):
# do stuff
else:
print('Incorrect formatting, try entering again')
Another option is:
def validString(string):
listofints = [v.isdigit() for v in string.split(",") if 0 < len(v) < 6]
if not len(listofints) or not all(listofints):
print("Incorrect formatting, try entering again.")
The following code should leave the Boolean variable valid as True if the entered ibnput complies with your rules and False otherwise:
n = input("Your input: ")
valid = True
try:
if int(n) > 9999:
valid = False
except:
for character in n:
if not character in "0123456789,":
valid = False
if n[-1] == ",":
valid = False

Python - Password Validation program

I am trying to make a program that carries out password validation when a user is making an account for this first time and has to enter a new password.
The password has to be more than 8 letters, contain an upper case letter, a lower case letter and a number.
I've done the following code, and it doesn't work at the moment. I think it's because islower() only works if all characters in the string are lower, same with isupper but for uppercase letters. How can I solve this issue, to get a nice clean solution for the requirements? Thanks
while loop:
pass1 = input('type a password: ')
if len(pass1) > 8 and pass1.islower() and pass1.isupper() and pass1.isdigit():
print('password successful')
break
else:
print('Password not valid, please retry')
Create another if else statement where you check islower method return true or false or set it equal to another boolean value then use that value in the second(which is your main if statement) statement then run it.
The condition that you are looking for is as follows: the password must be alphanumeric, but it must not consist of all lower-case letters, it must not consist of all upper-case letters, it must not consist of all letters, and it must not consist of all digits:
pass1.isalnum() and not pass1.isupper()\
and not pass1.islower()\
and not pass1.isalpha()\
and not pass1.isdigit()
This should work.
def anyLower(s):
return s.upper() != s
def anyUpper(s):
return s.lower() != s
def anyDigit(s):
return any(char.isdigit() for char in s)
while True:
userInput = input("Type a password: ")
if len(userInput) > 8 and anyLower(userInput) and anyUpper(userInput) and anyDigit(userInput):
print("Password successful")
break
else:
print("Password not valid, please retry")
anyLower() function converts the string to uppercase and checks if it changes, if it has changed, then the string must contain at least one lowercase character. anyUpper() works in a similar way.
anyDigit() iterates through all of the characters in the string and returns true if any of the characters are digits using the any() function.
You can try utilizing the built-in any() method along with the built-in map() method to check if any of the characters in the string, mapped with your choice of str method, returns True:
conditions = str.islower, str.isupper, str.isdigit
while True:
pass1 = input('type a password: ')
if len(pass1) > 8 and all(any(map(c, pass1)) for c in conditions):
print('password successful')
break
else:
print('Password not valid, please retry')
The password has to be more than 8 letters, contain an upper case letter, a lower case letter and a number.
It's good programming practice to code to functional requirements so it would be useful to have a specific function such as is_password_valid().
And, since the intent is to check if all the conditions are met, you can exit early before needing to check all the characters (possibly many times) as some of the other answers here do. In other words, something like (with a test harness):
import sys
def is_password_valid(password):
if len(password) <= 8:
return False
has_upper, has_lower, has_digit = False, False, False
for character in password:
if character.islower(): has_lower = True
elif character.isupper(): has_upper = True
elif character.isdigit(): has_digit = True
if has_lower and has_upper and has_digit:
return True
return False
chs = ['1', 'a', 'B', '_']
for ch1 in chs:
for ch2 in chs:
for ch3 in chs:
for ch4 in chs:
str1 = ch1 + ch2 + ch3 + ch4 + "....."
print(f"{str1} : {is_password_valid(str1)}")

Making a valid password checker. Can not get my program to get through the if conditions to print a valid password

I have been assigned the following exercise as homework:
Some Web sites impose certain rules for passwords. Write a function that checks whether a string is a valid password. Suppose the password rules are as follows:
A password must have at least eight characters.
A password must consist of only letters and digits.
A password must contain at least two digits.
Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise.
I know there's a more efficient and proper way of doing this but i'm just starting out so I don't necessarily need to be those right now. Just want to finish this question.
The counter/ accumulator works and I don't get any errors but I can not fulfill the if condition correctly so that this program prints "valid password"
password = str(input("Enter in a password to be checked: "))
def valid_password_checker(password):
from string import ascii_lowercase as alphabet
digits = '0123456789' # creates a string of digits
digit = 0 # acc for digits
length = 0 # acc for length
for char in password: # calls on each character in string
if char in alphabet:
length += 1
if char in digits:
digit += 1
if digit >= 2:
flag = True
if length >= 8 and digit is True:
print("valid password")
else:
print("Password does not contain enough characters or digits.")
else:
print("Password does not contain enough digits.")
valid_password_checker(password)
The problem with your existing code is that the variable digit is a number, and therefore doing digit is True as you have done in your if statement, always return False. If you remove digit is True, then your existing solution will work. Take a look however at my version:
def valid(password):
digits = 0
characters = 0
for char in password:
if char.isalpha():
characters += 1
elif char.isdigit():
digits += 1
characters += 1
if characters >= 8:
if digits >= 2:
print("Password is valid")
else:
print("Password doesn't contain enough digits")
else:
print("Password doesn't contain enough characters")
I have made the following modifications from your original:
Used the built-in function str.isdigit() for checking if a character is a digit.
Used the built-in function str.isalpha() for checking if a character is a letter of the alphabet
Moved everything but the counting operations outside of the for loop, so that the function doesn't print multiple things
If you want, you can undo the first two changes, if you're worried about your teacher knowing you asked for help. However, I wouldn't turn in the solution that prints "Password doesn't contain enough digits" as many times as there are characters in the inputted password.
you can write something like this:
password = str(input("What is the password that you want to validate: "))
def get_digits(password):
return [i for i in password if i.isdigit()]
numbers = ''.join(get_digits(password))
if (len(password) < 8) or (len(numbers) < 2):
print(password, "is an invalid password")
else:
print(password, "is a valid password")
nice and simple.

Python password strength [duplicate]

This question already has answers here:
Checking the strength of a password (how to check conditions)
(6 answers)
Closed 9 years ago.
I am programming a password strength code on python and i'm trying to find out if my password (p) contains a number, I have found out how to see if it contains upper and lower case letter by p.isupper() or p.islower(). I have also put them two together. My friend has told me how to see if the password only contains number but I need your help now.
running=True
while running:
p=raw_input("What is your Password? ")
if len(p) <6:
print "Your Password is too short"
if len(p) >12:
print "Your Password is too long"
if len(p) == 6 or 7 or 8 or 9 or 10 or 11 or 12:
print "Password Length OK"
running=False
print "Loop Broken" #this will be deleted, only for my help now
if p.isupper():
print "Your Password is weak as it only contains capital letters"
if p.islower():
print "Your Password is weak as it only contains lower case letters"
if p.isupper and p.islower:
print "Your Password is of medium strength, try adding some numbers"
try:
int(p)
print "Your Password is weak as it only contains numbers"
except (ValueError, TypeError):
pass
All I need now is the code for, if the password contains lower or upper case letters and numbers.
To me, regex would definitely be the easiest way of going about this.
Given a sample password password, the way you would check it would be:
import re
# Check if contains at least one digit
if re.search(r'\d', password):
print "Has a digit"
# Check if contains at least one uppercase letter
if re.search(r'[A-Z]', password):
print "Has uppercase letter"
# Check if contains at least one lowercase letter
if re.search(r'[a-z]', password):
print "Has lowercase letter"
For your other pieces, you can continue to use .isupper() and .islower().
By the way, this portion of your code:
if p.isupper and p.islower:
print "Your Password is of medium strength, try adding some numbers"
Will not function how you want it to. First, you're not actually calling the methods, since you haven't put the parentheses--you need to write if p.isupper() and p.islower():. Second, this doesn't actually do what you want it to. You're trying to check that it contains both lowercase and uppercase numbers. Instead, you're checking both that it is completely uppercase and completely lowercase, and obviously it can't be both, so that if statement will always return False. Instead, you'll want to use something like:
if re.search(r'[a-z]', password) and re.search(r'[A-Z]', password):
Or, alternatively, without re:
import string
if any(letter in string.ascii_lowercase for letter in password) and \
any(letter in string.ascii_uppercase for letter in password):
Or:
if any(letter.islower() for letter in password) and \
any(letter.isupper() for letter in password):
I happen to prefer re because it's more concise.
I guess you want to check whether your password contains all of these : lowercase, uppercase and digits.
>>> from string import ascii_lowercase, ascii_uppercase, digits
>>> s_lc = set(ascii_lowercase)
>>> s_uc = set(ascii_uppercase)
>>> s_d = set(digits)
def func(strs):
s = set(strs)
return all(s & x for x in (s_lc,s_uc,s_d ))
...
>>> func('fooBar09')
True
>>> func('fooBar')
False
>>> func('900')
False
>>> func('900aB')
True
Secondly len (p) == 6 or 7 or 8 or 9 or 10 or 11 or 12 is going to be evaluated as:
(len(p) == 6) or 7 so if len(p) is 6 then it returns True else 7(which is a Truthy value as well)
it should be : if 6 <= len(p) <= 12
>>> 7 or 8 or 9 or 10 or 11 or 12
7
Use while True instead of using a flag variable, and you need if-elif-else conditions here not if-if:
while True:
p=raw_input("What is your Password? ")
le = len(p)
if le <6:
print "Your Password is too short"
elif le >12:
print "Your Password is too long"
elif 6 <= le <= 12:
print "Password Length OK"
break

Categories