how to limit the input integer - python-3.x

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()

Related

how can I get a program to run if the user has inputted at least one character?

how can I get a get a program to run if the user has inputted at least one character. For example:
userinput = input("Enter your age: ")
if (user input) > (at least one character/number):
print(blank)
To clarify, I am wondering how if a user input has more than one character, how to make the if statement run (or any other statement)
you can use len() function to check the length of the input. so your code will look like.
userinput = input("Enter your age: ")
if len(userinput) >= 1:
print(userinput)
In this case if the input contains any character or space the if statement will work. If your looking for only character or digits then you can use the re module to check for the input contents. In this case you write your code as.
import re
userinput = input("Enter your age: ")
if re.search(r'[0-9A-Za-z]+', userinput):
print(userinput)
This regular expression will search for any digit or character and if found will set the condition to True.

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")

how would i create a program in python to implement this?

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.

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.

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