Question

(IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that...

(IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that you have been provided on Canvas. That file has a name and 3 grades on each line. You are to ask the user for the name of the file and how many grades there are per line. In this case, it is 3 but your program should work if the files was changed to have more or fewer grades per line. The name and the grades are separated by commas. You are to read in the file and place the student name and the average of the grades in a dictionary. Print out the dictionary after all the grades are processed. In order to get all the points for this assignment, you must have at least one function other than the main code, and the function must be commented beneath the def line with a docstring containing what the function does and any parameters that are required and any values returned. You must have meaningful variable names, and you must also read the file in a try/except block. You must also provide the code and the console output showing that your program enters all the lines from the Grades.txt files into a dictionary. You can use just the standard print function to print the dictionary. (IN PYTHON)

The following is what was EXACTLY in the file Grades-1.txt:

Fred, 57, 73, 98

George, 98, 77, 93

Donald, 81, 91, 71

JoAnn, 99, 88, 93

Margaret, 58, 79, 89

Steven, 90, 56, 47

Jamaal, 98, 91, 93

Davonte, 90, 70, 88

Jeri, 58, 78, 93

MaryEllen, 92, 88, 90

Patrick, 58, 62, 64

Derek, 90, 80, 70

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the following program in python to calculate the grade for the students in python and added necessary comments in it

Program:


def main():
"""
This api read the file Grades-1.txt using try and except block
and calculates the average of the grade
"""
  
filename=input("Enter the file name: ")
gradesNum=int (input("Enter the number of Grades in each line: "))
  
finalDict={}
total =0
  
try:
#open the file
f = open(filename, 'r')
with f:
lines=f.readlines()
#read line by line
for line in lines:
  
string=str(line)
lis1=string.split(',');
name=lis1[0]
print("Reading student: %s" %(name))
for i in range(gradesNum):
total=total + int(lis1[i+1])
#update the dictionary with average
finalDict[name]=average(total, gradesNum)
total=0
  

except OSError:
print("Unabel to open Grades-1.txt")
return
  
print("\n")
print(" %s %s" %("Student", "Average"))
for i in finalDict.keys():
print("%10s %.3f" %(i, finalDict[i]))

def average(total, gradesNum):
"""
This api calculates and return the average of the student
"""
avg = total/gradesNum
#return the average
return avg;
  
#call the main function
if __name__== "__main__":
main()

Output:

Enter the file name: Grades-1.txt
Enter the number of Grades in each line: 3
Reading student: Fred
Reading student: George
Reading student: Donald
Reading student: JoAnn
Reading student: Margaret
Reading student: Steven
Reading student: Jamaal
Reading student: Davonte
Reading student: Jeri
Reading student: MaryEllen
Reading student: Patrick
Reading student: Derek


Student Average
Jamaal 94.000
Derek 80.000
Davonte 82.667
Fred 76.000
Margaret 75.333
JoAnn 93.333
Donald 81.000
MaryEllen 90.000
George 89.333
Steven 64.333
Jeri 76.333
Patrick 61.333


Screen Shot:

Add a comment
Know the answer?
Add Answer to:
(IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Basic Lists, File IO, Exception Handling 1. Call createTable() to: Read in the data from...

    Python Basic Lists, File IO, Exception Handling 1. Call createTable() to: Read in the data from each of the input files (in order from scores1.txt to scores5.txt) and store the data in table. The table is a list of lists, where the scores of each input file is a column of data of the table. By the end of the function, the table should have 5 columns of numbers, each column is from one input file. Here is the first...

  • I need this in Python Please! The college IT department manager no longer wants to use...

    I need this in Python Please! The college IT department manager no longer wants to use spreadsheets to calculate grades. The manager has asked you to create a program that will input the teachers' files and output the students' grades. In your Python environment, the program must read an input file formatted in CSV format, named input.csv. Each record contains data about a student and their corresponding grades. The data will look similar to the following: Student Name, assignment 1,...

  • CSCI 145 program must be in python 3.0 grades.txt contains the final grades for students in...

    CSCI 145 program must be in python 3.0 grades.txt contains the final grades for students in CSCI 145. The dean would like to see a breakdown of who passed, who did not, and the total percentages of each group. Write a program that prompts for the file path to grades.txt, reads in grades.txt, and produces two files -- passed.txt and wdf.txt. passed.txt should contain the names of the students who received a grade of A, B, or C. wdf.txt should...

  • Python Assignment Tasks 1. Place the two provided plaintext files (file_1.txt, file_2.txt) on your desktop. 2....

    Python Assignment Tasks 1. Place the two provided plaintext files (file_1.txt, file_2.txt) on your desktop. 2. Write a Python program named fun_with_files.py. Save this file to your desktop as well. 3. Have your program write the Current Working Directory to the screen.   4. Have your program open file_1.txt and file_2.txt, read their contents and write their contents into a third file that you will name final.txt . Note: Ponder the open‐read/write‐close file operation sequence carefully. 5. Ensure your final.txt contains...

  • To answer this question you must submit a fully functional "C" program, this is your "main.c"...

    To answer this question you must submit a fully functional "C" program, this is your "main.c" file. This is the content of the Grades.txt file (download here) Adam 4.00 7.00 9.00 10.00 Catherine 10.00 8.00 1.00 9.00 John 10.00 9.00 7.50 6.50 Pedro 10.00 9.00 7.50 9.00 Wanda 9.00 6.50 4.50 10.00 In the file Grades.txt you will find that each line contains the name of a student and his 4 grades for the current term. Create a C program...

  • Using C programming For this project, you have been tasked to read a text file with student grade...

    Using C programming For this project, you have been tasked to read a text file with student grades and perform several operations with them. First, you must read the file, loading the student records. Each record (line) contains the student’s identification number and then four of the student’s numerical test grades. Your application should find the average of the four grades and insert them into the same array as the id number and four grades. I suggest using a 5th...

  • Python programming question Using an adjacency matrix: Take a txt file and have the program read...

    Python programming question Using an adjacency matrix: Take a txt file and have the program read it and output the contents into an adjacency matrix. - The txt file would have the size of the matrix and a list of names. - Print the filled matrix of the names - After each iteration, the program will ask, 'continue'? - Show how to swap names for the next iteration(Program would take an input of two names and swap them) Example: input:...

  • I'm a bit confused on how to get this program to run right. Here are the...

    I'm a bit confused on how to get this program to run right. Here are the directions: Part 1: Write a Python function called reduceWhitespace that is given a string line and returns the line with all extra whitespace characters between the words removed. For example, ‘This line has extra space characters ‘  ‘This line has extra space characters’ Function name: reduceWhitespace Number of parameters: one string line Return value: one string line The main file should handle the...

  • Python Modify your program from Learning Journal Unit 7 to read dictionary items from a file...

    Python Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following: How to format each dictionary item as a text string in the input file. How to covert each input string into a dictionary item. How to format each item of your inverted dictionary as a text string in the output file. Create an input file with your original...

  • write a program that uses a function to read in the grades for four classes and...

    write a program that uses a function to read in the grades for four classes and then uses a function to calculate the average grade in each of the classes. input the grades for one student. we will name her Beverly. output how Beverly's grade differs from each classe average. Make sure that your output is very clear. Below is your data: English 90 70 85 45 95 95 82 97 99 65 History 63 94 60 76 83 98...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT