154k views
0 votes
Write a program password.py that asks a user for a valid password. The program should keep asking for a password, until the user enters the right password. Here are the requirements for a valid password: 1- Must be between 4 and 8 characters (inclusive) 2- The first character of the password must be in uppercase 3- The last character of the password must be a number

User Parthagar
by
7.1k points

1 Answer

3 votes

Answer:

The program in Python is as follows:

password = input("Password: ")

while(True):

if len(password) >=4 and len(password) <=8:

if password[0].isupper():

if password[-1].isdigit():

break;

password = input("Password: ")

print("Correct Password")

Step-by-step explanation:

This gets input for the password

password = input("Password: ")

This loop is repeated while the password does not meet the 3 rules

while(True):

This checks for length

if len(password) >=4 and len(password) <=8:

This checks if first character is upper case

if password[0].isupper():

This checks if last character is a digit

if password[-1].isdigit():

If the conditions are met, the loop is exited

break;

This gets input for password, if any of the conditions fail

password = input("Password: ")

This ends the program

print("Correct Password")

User Xpilot
by
7.3k points