163k views
3 votes
Write a program that uses a list that contains five (5) user names and another list

that contains respectively their five (5) passwords. The program should ask the
user to enter their username and password. If the username is not in the first list,
the program should indicate that the person is not a valid user of the system. If
the username is in the first list, but the user does not enter the right password,
the program should say that the password is invalid. If the password is correct,
then the program should tell the user that they are now logged into the system.

2 Answers

6 votes

Final answer:

To write a program that validates user login credentials, create two lists for usernames and passwords. Prompt the user for their credentials and use conditional statements to check for validity. Display appropriate messages based on the results.

Step-by-step explanation:

To write a program that validates user login credentials, create two lists: one for usernames and another for passwords. Then, prompt the user to enter their username and password. Use conditional statements to check if the username is in the list of valid usernames. If it is not, display a message indicating that the user is not a valid user. If the username is valid, check if the corresponding password matches the one provided by the user. If the password is incorrect, display a message stating that the password is invalid. Otherwise, inform the user that they are logged into the system.



Example:



usernames = ['user1', 'user2', 'user3', 'user4', 'user5']
passwords = ['pass1', 'pass2', 'pass3', 'pass4', 'pass5']

username_input = input('Enter your username: ')
password_input = input('Enter your password: ')

if username_input not in usernames:
print('Invalid username')
else:
user_index = usernames.index(username_input)
if password_input == passwords[user_index]:
print('Logged into the system')
else:
print('Invalid password')

User Pinhead
by
7.5k points
3 votes

Answer: Here's the program in Python:

```

# create lists of user names and passwords

usernames = ['john', 'mary', 'dave', 'jane', 'alex']

passwords = ['pass123', 'abc456', 'qwerty', 'password', 'letmein']

# ask user to enter their username and password

username = input("Enter your username: ")

password = input("Enter your password: ")

# check if username is valid

if username not in usernames:

print("Invalid username.")

else:

# get the index of the username in the list

index = usernames.index(username)

# check if password is valid

if password == passwords[index]:

print("You are now logged in.")

else:

print("Invalid password.")

Explanation: In this program, we first create two lists `usernames` and `passwords` that store the user names and passwords respectively. We then ask the user to enter their username and password using the `input()` function.

Next, we check if the entered username is valid by using the `not in` operator to check if the username is not in the `usernames` list. If the username is invalid, we print a message saying so.

If the username is valid, we get the index of the username in the `usernames` list using the `index()` method. We then check if the entered password matches the password stored at that index in the `passwords` list. If the password is valid, we print a message saying that the user is now logged in. If the password is invalid, we print a message saying so.

User Phnah
by
9.0k points