Question: Write a program to read the contents of the file into a list. The program will then prompt the user for a username and password.

Write a program to read the contents of the file into a list. The program will then prompt the user for a username and password. Finally, we will tell the user whether the user is authenticated.

Read From a File

The first part of the program will be to read the contents of a file into a list. This data will be in JSON. For example, the contents of the file Lab02.json might be:

{ "username": [ "King Arthur", "Lancelot", "Sir Robin, the Not-Quite-So-Brave", "Black Knight", "Sir Bedivere", "Roger the Shrubber", "Brother Maynard", "Bridgekeeper", "French Soldier", "Knight of Ni", "Dead Collector", "Dennis", "King of Swamp Castle" ], "password": [ "Run away!", "She turned me into a newt!", "That's enough singing for now", "None shall pass", "How do you know so much about swallows?", "Oh, what sad times are these when passing ruffians can say Ni at will to old ladies.", "Armaments, chapter two, verses nine through twenty-one", "What... is the air-speed velocity of an unladen swallow?", "I fart in your general direction", "You must cut down the mightiest tree in the forest... WITH... A HERRING!", "Bring out your dead!", "Help, I'm being oppressed", "Let's not bicker and argue over who killed who" ] }

The program will open Lab02.json (which you will have to download onto your computer). If the file does not exist or the program is otherwise unable to open it, then present the user with a user-friendly error:

Unable to open file Lab02.json.

Covert to List

The next step is to convert the JSON data into a list. This is accomplished in a three-step process:

  1. Read the data from the file into a single string.
  2. Convert the string into a JSON object.
  3. Convert the username and password components of the JSON object into two lists.

Find the Username and Password

The final part of the exercise is to prompt the user for a username and password. You will then search the usernames and passwords lists to see if the username and password are on the lists. If both are present and at the same location in the two lists, then the user is considered authenticated.

Example

The following example is a run-through of the program. First, a demonstration of an incorrect username:

Username: John Cheese Password: None shall pass You are not authorized to use the system.

Next, a demonstration of an incorrect password:

Username: Black Knight Password: Tis but a scratch. You are not authorized to use the system.

Finally, a demonstration of the correct username and password:

Username: Black Knight Password: None shall pass You are authenticated!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!