how would i create a program in python to implement this? - python-3.x

A password must have at least eight characters.
• A password consists of only letters and digits.
• A password must contain at least two digits

you can refer to this code as your answer :
import re
def validate():
while True:
password = input("Enter a password: ")
if len(password) < 8:
print("Make sure your password is at lest 8 letters")
elif re.search('[0-9]',password) is None:
print("Make sure your password has a number in it")
elif re.search('[A-Z]',password) is None:
print("Make sure your password has a capital letter in it")
else:
print("Your password seems fine")
break
validate()

if you want to generate such passwords use this code:
import string
import random
letters =string.ascii_letters
digits = string.digits
comb = letters + digits
length = random.randint(6,98)
password = random.choices(digits,k = 2)
password+= random.choices(comb, k =length)
random.shuffle(password)
password = ''.join(password)
print(password)
I assumed the max length of a password is 100. you may want to change it.

Related

Why does my if statement return the wrong prompt?

I am sure the answer is right in front of my face but I can't seem to figure out how to fix the return on my first if statement when I input the right conditions.
create_password = input("Enter password here: ")
if len(create_password) > 6 and create_password.isdigit() > 0:
print("Your account is ready!")
elif len(create_password) < 6 and create_password.isdigit() < 1:
print("Password must be more than 6 characters and must include a number")
elif len(create_password) > 6 and create_password.isdigit() == 0:
print("Password must include a number")
else:
print("Your password sucks")
Let's say I enter elephant100, I am trying to get the prompt to be "Your account is ready!". But to my dismay, it prints "Password must include a number" and I cannot figure out why. My other conditions match the right input but that is the only one that does not work.
.isdigit() method returns True if all the characters are digits, otherwise False. Hence it returns False in this case since your string contains letters like e, l, p etc. So the statement print("Your account is ready!") will never be executed.
Turns out I needed to use isalnum(), isnumeric(), and isalpha() to fix my problem. Thank you Muhammed Jaseem for helping me figure it out! Here is my revised code.
if create_password.isnumeric() == True:
print("Password must be more than 6 characters and include letters")
elif create_password.isalpha() == True:
print("Password must be more than 6 characters and include numbers")
elif len(create_password) > 6 and create_password.isalnum() == True:
print("Your account is ready!")
else:
print("Your password sucks. Must be more than 6 characters and contain only letters and numbers")

noob horribly stuck on what is most likely an easy question

Flowchart
Trying to create a password generator
Cannot get if statement to work correctly, or know if this is really the right way to approach the problem. I need it to divide each letters number representation by 3 and return a # if it is a whole number.
password = input("password: ")
password = password.lower()
output = []
for character in password:
number = ord(character) - 96
output.append(number)
x = output
if x / 3:
print ("#")
print (output)
I get this error:
TypeError: can only concatenate list (not "int") to list
I don't know what you are trying to do with the divisible by 3. To get you started, here's an example code. See if this helps you get started in the right direction.
password = input('enter password :').lower()
output = []
for c in password:
num = ord(c) - 96
output.append(num)
all_div_by_3 = True
for i in output:
if i%3 != 0: #checks if remainder of i/3 is zero. if zero, then divisible, else not divisible.
all_div_by_3 = False
break
if all_div_by_3: #is same as if all_div_by_3 == True:
print ('all divisible by 3')
else:
print ('all characters are not divisible by 3')
The output from this are as follows:
enter password :cliff
all divisible by 3
enter password :rock
all characters are not divisible by 3
After much reading and research through here it became apparent I needed to use the if, elif, and else functions. below is the completed project.
password = input("password: ")
password = password.lower()
output = []
for character in password:
number = ord(character) - 96
output.append(number)
for i in output:
if(i% 3 == 0) :
print('#', end ="")
elif(i% 5 == 0) :
print('%', end ="")
else:
print(chr(i+98), end="")

Python3 if if else

When i do this in this way
x = "Hello"
if len(x) <= 9:
print("The password must contain at least 9 letters")
if x[0].islower():
print("The first password letter must be uppercase")
else:
print("Password saved")
password = x
i getting
>The password must contain at least 9 letters
>Password saved
What should I do to make the program stop on:
>The password must contain at least 9 letters
Use elif between if and else:
x = "Hello"
if len(x) <= 9:
print("The password must contain at least 9 letters")
elif x[0].islower():
print("The first password letter must be uppercase")
else:
print("Password saved")
password = x
elif is executed only when if wasn't executed, and elif's condition is true. You can also chain as many elifs as you want, in which case the first elif whose condition matches is executed.
Update: Since OP said in comments that he wants all errors to be shown at once, I would use something like this:
x = "Hello"
errors = []
if len(x) <= 9:
errors.append("The password must contain at least 9 letters")
if x[0].islower():
errors.append("The first password letter must be uppercase")
if errors:
print('\n'.join(errors))
else:
print("Password saved")
password = x
The problem is that you have two if filters in the code. I'm assuming you want the structure where both "The password must contain at least 9 letters" and "The first password letter must be uppercase"can be returned if both their conditions are met.
If, however, you don't need this capability, simply replace the second if with an elif and it should work.
If you need this capability, try something like:
x = "Hello"
if len(x) <= 9:
print("The password must contain at least 9 letters")
if x[0].islower():
print("The first password letter must be uppercase")
if len(x) >= 9 and x[0].isupper():
print("Password saved")
password = x
This simply adds a third if statement testing that the previous conditions were fulfilled.

how to limit the input integer

I'm trying to make a program that will determine the password if its valid or not! My problem is what code i need to use to limit the input integer of the user.
here is the requirement for the password
• A password must have at least eight characters.
• A password consists of only letters, digits and underscore.
• A password must contain at least two digits numbers.
def password():
password = input ('Please Type your Password: ')
_ = "_"
if len(password) <=7 or set('[~!##$%^&*()+{}":;\']+$').intersection(password) <=1:
print ("You Enter a Invalid Password")
else:
len(password)>=8 and password.lower()== password and password.upper()==password and password.isalnum()==password and set(_).intersection(password)
print ("You Enter a Valid Password")
password()

Checking an input for characters $#!%&*_ python

No i'm not cursing in the title. i need to create a password processing program that checks a input on whether it meets certain criteria one of which is that it must contain one of the characters $#!%&*_.
this is what i currently have.
def pword():
global password
global lower
global upper
global integer
password = input("Please enter your password ")
length = len(password)
lower = sum([int(c.islower()) for c in password])
upper = sum([int(c.isupper()) for c in password])
integer = sum([int(c.isdigit()) for c in password])
def length():
global password
if len(password) < 8:
print("Your password is too short, please try again")
elif len(password) > 24:
print("Your password is too long, please try again")
def strength():
global lower
global upper
global integer
if (lower) < 2:
print("Please use a mixed case password with lower case letters")
elif (upper) < 2:
print("Please use a mixed case password with UPPER case letters")
elif (integer) < 2:
print("Please try adding numbers")
else:
print("Strength Assessed - Your password is ok")
This kind of thing will work:
required='$#!%&*_'
def has_required(input):
for char in required:
if input.contains(char):
return True
return False
has_required('Foo')
must_have = '$#!%&*_'
if not any(c in must_have for c in password):
print("Please try adding %s." % must_have)
any(c in must_have for c in password) will return True if any one of the characters in password is also in must_have, in other words, it will return True is the password is good. Because want to test for bad passwords, we put not in front of it to reverse the test. Thus the print statement is executed here only for bad passwords.
You can achieve this easily with a list comprehension + the any() built-in.
has_symbol = any([symbol in password for symbol in list('$#!%&*_')])
Or a little more broken up:
required_symbols = list('$#!%&*_')
has_symbol = any([symbol in password for symbol in required_symbols])

Resources