This seems like a homework assignment so I'll try and stay from directly answering, but more try to push you in the right direction.
Your first bit of code that will run will be
...
def main():
password = validPassword() # the password we are trying to check
...
Uh oh, validPassword(password)
takes an argument and doesn't get a password, maybe you meant getPassword()
Lets go through the logic of validPassword(password)
Line by line
...
def validPassword(password):
if len(password) >= 8:
valid = True
...
lets check if the length of the string is more than 8 characters, if it is, we initialize the variable valid and set it to True
...
if password.alnum():
valid = True
...
Then regardless of what has happened, we call alnum, ( which I don't think is a function in python, probably meant isalnum. ) to check if all the characters in the password are numbers.
If it is we initialize the variable valid and set it to True.
You may say, but I already initialized it, well not really, in python there is scope.
...
if password.isdigit < 2:
valid = True
...
Then we check if the passwords method isdigt
is less than 2, maybe you meant password.isdigit()
I am really being meticulous as it is unclear your proficiency in programming or python. But if you meant password.isdigit() < 2
then you are asking if the password is a digit and if yes, is it less than 2.
If it is we initialize the variable valid and set it to True.
...
else:
return False
...
Then if and only if password.isdigit() < 2
is false, we return false.
Here are some pointers:
- Learn about control flow in python
- When you ask a question here, rather than say, "here is problem, here
is my current code, please help," say, "here is problem, here is my
current code, this is what I expect on line x,y,z and a,b,c is
happening, please help" if we do not know where you are struggling,
how can we help you best
- Try and run your code and show us the stacktrace ( if it exists ), there are definitely errors here that would have come up, python happens to have nicer errors that most languages (in my opinion)
Hopefully my line by line explanation has helped you find some of your errors and a better idea of how to continue, if not, feel free to amend your question so that we get a better idea of how to help.
Happy coding.