0

Write a function that checks whether a string is valid password.

Rules: Must have at least 8 characters A password must consist of only letters and digits A password must contain at least 2 digits

Heres what I have so far, what am i doing wrong? thank you

def getPassword():
   password = input("Enter password: ")
   return password

def validPassword(password):
   if len(password) >= 8:
      valid = True
   if password.alnum():
      valid = True
   if password.isdigit < 2:
      valid = True
   else:
      return False


def main():
   password = validPassword()
   if validPassword(password):
      print(password, "is valid")
   else:
      print(password, "is invalid")
main()
Hassan Ali
  • 1
  • 2
  • 5
  • Please tell us what it is *you* are doing wrong (include a stacktrace, if any) so we can help. What is happening now that you don't expect? – idjaw Apr 03 '16 at 00:41

2 Answers2

1

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.

0

According to the following reference here for the isdigit() method :

This method returns true if all characters in the string are digits and there is at least one character, false otherwise.

Which doesn't hold for your case

A password must contain at least 2 digits

The method will only let you know if the given string is a digit, not how many digits are in a string. To achieve that you will need to play around a bit.

You may use the following

if sum(character.isdigit() for character in password) >= 2:

Furthermore, your code has a small mistake as you will never return back True. Here is a possible fix:

def CountDigitsFor(password):
   return sum(character.isdigit() for character in password)

def validPassword(password):
   if len(password) >= 8 and password.isalnum() and CountDigitsFor(password) >= 2:
      return True
   return False

Additioanly, in your main you have a small typo when getting the password from the user

password = validPassword()

Should be

password = getPassword()

Therefore here is a complete code

def getPassword():
   return input("Enter password: ")

def CountDigitsFor(password):
   return sum(character.isdigit() for character in password)

def validPassword(password):
   if len(password) >= 8 and password.isalnum() and CountDigitsFor(password) >= 2:
      return True
   return False

def main():
   password = getPassword()
   if validPassword(password):
      print(password + " is valid")
   else:
      print(password + " is invalid")

main()
Rafael
  • 5,613
  • 4
  • 36
  • 44