196k views
0 votes
Write a program that will create a student database using dictionary.

The database will store studentId, and name. The studentId will be key and name will be value for the dictionary.

The text file will have the studentID and student name, which are separated by comma(,). The program will read the file and populate the dictionary. The studentID will be the key and name will be the value.

The program will ask the user to enter a studenID. It will search the dictionary. If found, it will display the name; otherwise, it will say, “Student Not found” (LOOK AT PIC BELOW PLS, PYTHON)

Write a program that will create a student database using dictionary. The database-example-1

1 Answer

3 votes

Hello, below are the codes I wrote in Python. I defined each function and its purpose by explaining them with comments. Feel free to ask me any questions or clarifications if you get stuck or don't understand anything. Additionally, you can see the data I used in the images I added below, and you can revise it according to your own needs.

Python:

#Import regex library

import re

#Check the user prompt if the student ID is legal.

def getStringData(prompt):

return "Y" if(re.match(r'^[A-Z]\d[A-Z]\d$', prompt)) else "N"

#Reading data from "data.txt" and parsing to the dictionary.

def readData(data):

return {temp.split(',')[0]: temp.split(',')[1].strip() for temp in open(data).readlines()}

#Searching on database and print if found.

def displayResult(id, database):

return (f"Name: {database[id]}") if(id in database) else "Student not found on database."

def main():

#Load "data.txt" first.

database = readData("data.txt")

while(True):

#Get user input.

prompt = input("\\Enter student ID (Press 'n' to quit.): ")

if prompt.lower() == 'n':

break

#Check if input is valid student ID.

if getStringData(prompt) == "N":

print("Invalid student ID.")

continue

#Fetch result.

print(displayResult(prompt, database))

if __name__ == "__main__":

main()

Write a program that will create a student database using dictionary. The database-example-1
Write a program that will create a student database using dictionary. The database-example-2
User Riyas TK
by
8.2k points