120k views
3 votes
Complete the code to check the length of a password.

When done, valid should be True if the password is between eight and 20 characters long.


Valid will be False if it is not.


valid = true

password = input("Password? ")

lengthPassword = _____

(password)

if lengthPassword _____

20:

valid = False

elif lengthPassword _____

8:

valid = False


1. len, size, length

2. >, <

3. >,

1 Answer

3 votes

Answer:Here's the completed code to check the length of a password:

valid = True

password = input("Password? ")

lengthPassword = len(password)

if lengthPassword > 20:

valid = False

elif lengthPassword < 8:

valid = False

Step-by-step explanation:

The len function is used to get the length of the password. If the length is greater than 20 or less than 8, valid is set to False. Otherwise, valid remains True.

Note that the if statement uses the comparison operators > and < to check if the length of the password is greater than 20 or less than 8, respectively.

User Diego Perez
by
7.3k points