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()