Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE HELP ME! In Python, Create a program that manages a text file containing student records. Each record will contain a student name and 3

PLEASE HELP ME!

In Python, Create a program that manages a text file containing student records. Each record will contain a student name and 3 exam scores. Each field in a record is separated by a comma (no space), and the name is given as first name, a space, then last name. Here are 3 example records. Students.txt

linda tyler,76,72,88

dave smith,72,91,75

steve davis,88,92,84

Program needs to use a menu to allow the user to select one of 4 choices.

Menu options. Choose 1, 2, 3, or 4

1. Display student names and scores

2. Add a new student.

3. Display exam averages

4. Save and exit

Enter your choice : 1, 2, 3, or 4:

Program needs to start in a main() function where it will read the data from a file into a dictionary. This dictionary will hold all of the student records.

After loading the records from the file, the dictionary will have entries that have each student's name as a key, and the value associated with this key will be a list of integers containing the student's 3 exam scores. So, using first example record listed earlier, if the first record read was linda tyler and this code reads the file then the effect here would be to assign "linda tyler" as a key in the students dictionary, and have linda's 3 integer scores stored as a list to be the value associated with that key. It would be equivalent to this.

students["linda tyler"] = [76, 72, 88]

Each menu option should call a separate function, and here is what the options in the menu should do.

If you select option 1 a function will be called that displays the name and 3 scores for all students in the file, one on each row, no matter how many students are in the file.

if you select option 2 you call a function that allows the user to enter a name and 3 integer scores, and these values are to be added to the dictionary using the name as a key and the value a list of 3 scores.

If you select option 3 your function should print each student's name and the average of the scores for that student. For this option it is not necessary to display the scores themselves, although you can if you want. But the name and averages for sure.

if you select option 4 your function takes each dictionary item and converts it back to string record form, each field separate by a comma, and stored back to the file in the same format that it was originally stored in. This means using a comma separator and each record stored in a separate line. Putting the records back the way you found them means the program can be run a second time without an error.

In addition to the main() function and the function you call to read the data from the file, you will need 4 axillary functions, one function for each of the menu options. Also you must use a dictionary as described above to hold the data while it is in your program.

THIS IS WHAT I HAVE SO FAR

def loadRecords():

f = open("students.txt", "r")

students = {} # create an empty dictionary

for record in f.readlines():

name, s1, s2, s3 = record.split(",") # unpack & separate fields by comma

s1 = int(s1)

s2 = int(s2)

s3 = int(s3)

students[name] = [s1, s2, s3]

f.close()

print (students)

# return students

def displayStudents(students):

n = 1

for record in students:

name, s1, s2, s3 = record.split(",") #unpack record

students[name] = [s1, s2, s3]

return students

n += 1

def addStudent():

newRecord = [fn+ln],[s1,s2,s3]

fn = input("Enter student's first name: ")

ln = input("Enter student's last name: ")

s1 = float(input("Enter student exam grade: "))

s2 = float(input("Enter student exam grade: "))

s3 = float(input("Enter student exam grade: "))

return newRecord

def displayAvg(students):

dict = loadRecords(students)

for num in records:

sum=0

grades = record.split(",")

for num in grades:

sum = sum + int(num)

avg = sum/3

print ("students") (avg)

def saveRecords(students):

f = open("students.txt", "w")

f.writelines(students)

f.close

def main():

# students is a list of strings, each string a record

students = loadRecords() # load students from file

while True:

print("""

Program Options.

1.) Display student names and scores

2.) Add a new student

3.) Display exam averages

4.) Save and exit

""")

option = input("Enter 1, 2, 3, or 4: ")

print()

if option == "1":

displayStudents(students)

elif option == "2":

newRecord = addStudent()

newRecord.append(students) # add record to list

elif option == "3":

displayAvg(students)

elif option == "4":

saveRecords(students)

break # exit program

else:

print("Not a valid entry. Please enter 1, 2, 3 or 4.")

main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions