Reputation: 23
I'm trying to write code that can check if an input contains;
I keep getting stuck in a "inputs password, returns true, and input password again, exit" single loop..
Fairly new at programming, doing my first semester atm so all help would be appreciated!
This is my program so far
def is_valid():
valid = 0
password = input("Password: ")
for ele in password:
if ele.isupper and ele.islower and ele.isdigit and len(password) > 7:
return "True"
else:
return "False"
print(is_valid())
is_valid()
I tried moving the print inside the function, as I think it is intended, by then It won't print..
Upvotes: 2
Views: 453
Reputation: 2076
You're going element-wise in a loop on your password, rather than considering the whole thing together. You want something like:
def is_valid():
password = input("Password: ")
if not any(el.isupper() for el in password):
return False
if not any(el.islower() for el in password):
return False
if not any(el.isdigit() for el in password):
return False
if len(password) < 8:
return False
return True
is_valid()
Upvotes: 1
Reputation: 26976
You should also check for characters that are neither letters nor digits (according to the rules stated in the question) - e.g., punctuation.
How about using a bit mask?
def is_valid(pwd):
state = 0
if len(pwd) >= 8:
for c in pwd:
if c.isdigit():
state |= 1
elif c.islower():
state |= 2
elif c.isupper():
state |= 4
else:
state |= 8
return state == 7
password = input('Password: ')
print(is_valid(password))
Upvotes: 1
Reputation: 152
There are many ways to do this. But first I will clarify some mistakes.
First, when you write ele.isupper
you are not calling the function, for that you must put the parenthesis: ele.isupper()
.
Secondly, your loop is looping through each letter of the password and in the case of solving the previous bug you would find that the condition would never be fulfilled since a character cannot be all things at the same time.
I leave you an example of how you can solve these problems, as I said, there are many ways to do it but I present you one that is not complex and uses the basics. Also, if the password is incorrect, ask for it again on the screen.
def is_valid():
valid = [0, 0, 0]
password = input("Password: ")
for ele in password:
if len(password) < 8:
break
elif ele.isupper() and valid[0] == 0:
valid[0] = 1
elif ele.islower() and valid[1] == 0:
valid[1] = 1
elif ele.isdigit() and valid[2] == 0:
valid[2] = 1
if sum(valid) < 3:
print('False')
is_valid()
else:
print('True')
is_valid()
Output:
Password: hello
False
Password: Hello
False
Password: Hello World
False
Password: Hello World 1
True
The code first checks if the length is correct, if it is not, it does not make any further checks.
If this condition is met it continues and as conditions are met, one of the variables in the valid
list is incremented. You can do this with a number instead of a list, but if you want to specify what has gone wrong you can access each value in the list to check it or say that n conditions have failed, for example:
if valid[1] == 0:
print('There must be at least one lower character').
Upvotes: 1
Reputation: 108
There are a couple of problems in your code.
valid = 0
seems to be never used.print(is_valid())
would also have no effect.One possible straightforward solution to your problem would be to set a number of different flags for things you want to check, ant then once you find them, set te proper value for the flags. Like this:
def is_valid():
password = input("Password: ")
has_upper = False
has_digit = False
has_lower = False
has_good_length = False
for ele in password:
if ele.isupper():
has_upper = True
if ele.islower():
has_lower = True
if ele.isdigit():
has_digit = True
if len(password) > 7:
has_good_length = True
if has_upper and has_lower and has_digit and has_good_length:
return True
return False
print(is_valid())
Upvotes: 1
Reputation: 33335
for ele in password:
if ele.isupper and ele.islower and ele.isdigit and len(password) > 7:
return "True"
else:
return "False"
This code has several problems.
First, you're referring to the ele.isupper
function, but because you don't have parentheses ()
, you're not calling the function. So the code is basically asking "Does ele.isupper
exist"? Well yes, it is a function, and it exists, so the if
condition is true.
Use ele.isupper()
instead of ele.isupper
.
Second, even if you fix that problem (and also the same problem for ele.islower
and ele.isdigit
), there's no way that one letter will be uppercase AND lowercase AND a digit.
Third, the return
statement makes the function exit immediately, so your loop will only look at the first letter. Instead of doing that, you want to loop over all the letters, and move the return
statement to after the loop.
I think you were actually looking for code like this:
uc = 0
lc = 0
digits = 0
for ele in password:
if ele.isupper():
uc += 1
elif ele.islower():
lc += 1
elif ele.isdigit():
digits += 1
if uc > 1 and lc > 1 and digits > 1 and len(password) > 7:
return "True"
else:
return "False"
Upvotes: 2
Reputation: 65
for ele in password will iterate through the characters in the user's input.
your if statement doesnt make sense. ele.isupper and ele.islower will never be true at the same time.
if statement needs work. make booleans for each condition you want to validate and set them to true individually is you see the required elements.
Upvotes: -1