-2

Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.

I have come up with somewhat of a solution but cannot seem to find if a digit is involved in the input. This is what I have come up with so far.

Can you please help me how to check if there is a digit in the password in the input? There are question marks where I feel I should put something. Thank you!

def enterNewPassword():

    password = input("Enter a password: ")

    if len(password) < 8:

        print("Your password does not contain 8-15 characters.")

    if len(password) > 15:

        print("Your password contains more than 15 characters.")

    if ??? not in password:

        print("Your password does not contain a digit.")

    if ??? in password and 8 <= len(password) >= 15:

        print("Good password!")

enterNewPassword()
mb1
  • 21
  • 4

3 Answers3

1

If you want to check for a digit in a string you can use the any() method.

any(c.isdigit() for c in password)

any will pretty much return True if the condition that is being checked returns True at least once, in this case with the "c.isdigit()"

The isdigit() is a method available in your string object, so you are pretty much checking each character is a digit with that call. Here is a doc on isidigit as well.

Here is the doc on any()

idjaw
  • 25,487
  • 7
  • 64
  • 83
0
def enterNewPassword():

    while True:    # infinite loop
        s = input("\n\nEnter password: ")
                                 # count digits in string
        if 15 < len(s) < 8 or sum(str.isdigit(c) for c in s) < 1:
            print("Password must be 8-15 chars long and contain at least one digit:")
            continue
        else:
            print("The password is valid.")
            break

enterNewPassword()

Enter password: arte,alk;kl;k;kl;k;kl
Password must be 8-15 chars long and contain at least one digit:

Enter password: sunnyday
Password must be 8-15 chars long and contain at least one digit:


Enter password: rainyday
Password must be 8-15 chars long and contain at least one digit:


Enter password: cloudyday1
The password is valid .
LetzerWille
  • 5,355
  • 4
  • 23
  • 26
-1
def enterNewPassord():
while True:
   respond= input("Please Enter the  password has 8-15 characters, including at least one digit")
   hasDigit=False

   for i in respond:
       if i.isdigit():
           hasDigit=True
           break
     
   if not hasDigit or (len(respond) < 8 or len(respond) > 15):
       if len(respond) < 8 or len(respond) > 15:
           print("The password length must be between 8 and 15!")
           if not hasDigit:
               print("The password must include at least one digit!")
   else:
       print(respond)

   break
       
enterNewPassord()
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 30 '22 at 08:47