Question

Homework Assignment 5 Instructions: Class name must be: HW5_yourName For example: Michael will name the python...

Homework Assignment 5 Instructions: Class name must be: HW5_yourName For example: Michael will name the python file of homework assignment 5 as HW5_Michael.py Grading Rubric: Code running and as per the required conditions and giving expected output = 10 points File named as per instructions = 1 point Comments in code = 4 points Problem: Average calculation of test scores using a Dictionary Write a program that reads a text file named Asg5_data.txt and stores the name of the student as the key of the dictionary element. The scores of each student will be stored in a list and the list will be the value of the key of Dictionary element. The dictionary with key-value pair is shown in Expected Output. The program should display average (average of 2 tests) for each student. Expected Output: Dictionary of student and their test scores is: {'michael': [25, 23], 'adelle': [26, 28], 'james': [24, 26], 'anders': [23, 29]} Dictionary of student and their average test scores is: {'michael': 24.0, 'adelle': 27.0, 'james': 25.0, 'anders': 26.0}

Homework Answers

Answer #1

"""
Python version : 3.6
Python program that reads a text file named Asg5_data.txt and stores the name
and the list of scores into the dictionary where keys are names of students and
values are marks of the students. Then calculate and display the sictionary of
average scores of each student.
"""

# open input file in read mode
file = open("Asg5_data.txt","r")
# create an empty dictionary
students = {}

# read a line from file
line = file.readline()

# loop to read till the end of file
while line:
   # convert the string into list of strings
   # strip is used to remove leading and trailing whitespace from line string
   # split is used to split the string using whitespace as the delimiter
   data = line.strip().split()
   # extract the scores from data into score list
   score = []
   for i in range(1, len(data)):
       score.append(int(data[i]))
   # insert the data into the dictionary, where data[0] contains the name of student and score is the list of scores
   students[data[0]] = score
   # read the next line
   line = file.readline()
# close the file  
file.close()
# display the dictionary
print('Dictionary of student and their test scores is:\n',students)

# create an empty dictionary to store the average scores
avg_score = {}
# loop to calculate and insert average scores for each student into the dictionary
for name in students:
   avg_score[name] = float(sum(students[name]))/len(students[name])
# display the average score dictionary  
print('Dictionary of student and their average test scores is:\n',avg_score)  
#end of program  

Code Screenshot:

Output:

Input file: Asg5_data.txt (values in a line are separated by a single space)

Output:

Know the answer?
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
Similar Questions
COSC 1436 Programming Assignment 2 Programming Assignment 2 Refactoring is the process of taking existing code...
COSC 1436 Programming Assignment 2 Programming Assignment 2 Refactoring is the process of taking existing code and improving its design without changing the functionality. In this programming assignment, you are going to take your code from Programming Assignment 1 and refactor it so that it uses functions. The program should still do the following:  Get the name of a student  Get the name of three assignments  Get the grade of three assignments as integers  Calculates the...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments,...
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments, hands-on labs, discussions, and assignments to date. Create a Java class named HeadPhone to represent a headphone set. The class contains:  Three constants named LOW, MEDIUM and HIGH with values of 1, 2 and 3 to denote the headphone volume.  A private int data field named volume that specifies the volume of the headphone. The default volume is MEDIUM.  A private...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm to work along with a worked exercise from Chapter 5. Use two different looping strategies for different purposes. Prior Task Completion: 1. Read Chapter 05. 2. View Chapter 05’s video notes. 3. As you view the video, work along with each code sample in PyCharm using the Ch 5 Iteration PPT Code Samples.zip. Important: Do not skip the process of following along with the...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
PUBH 6033—Week 7 Assignment 1 Comparing two means: When drink drove a student to statistics (Rubric...
PUBH 6033—Week 7 Assignment 1 Comparing two means: When drink drove a student to statistics (Rubric included) Instructions For this assignment, you review this week’s Learning Resources and then perform a two-sample independent t test and an ANOVA related to the dataset that was utilized in the week 2 SPSS application assignment. Import the data into SPSS; or, if you correctly saved the data file in Week 2, you may open and use that saved file to complete this assignment....
PUBH 6033—Week 7 Assignment 1 Comparing two means: When drink drove a student to statistics (Rubric...
PUBH 6033—Week 7 Assignment 1 Comparing two means: When drink drove a student to statistics (Rubric included)                                              Instructions For this assignment, you review this week’s Learning Resources and then perform a two-sample independent t test and an ANOVA related to the dataset that was utilized in the week 2 SPSS application assignment. Import the data into SPSS; or, if you correctly saved the data file in Week 2, you may open and use that saved file to complete this...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
You are working for a company that is responsible for determining the winner of a prestigious...
You are working for a company that is responsible for determining the winner of a prestigious international event, Men’s Synchronized Swimming. Scoring is done by eleven (11) international judges. They each submit a score in the range from 0 to 100. The highest and lowest scores are not counted. The remaining nine (9) scores are averaged and the median value is also determined. The participating team with the highest average score wins. In case of a tie, the highest median...
Question 1: Group by and Aggregates: Write SQL statements to answer the following questions using Assignment...
Question 1: Group by and Aggregates: Write SQL statements to answer the following questions using Assignment 4’s schema (Customer-Invoice-Line-Product-Vendor). Make sure that your SQL script runs without any errors. Submit your answers in a .SQL file. 1 (2 Points) - Find the count of distinctvendors thatsupplied products that are priced lowerthan 185? 2 (2 Points) - For each vendor, find their product that has the lowest product quantity. Your output should include vendor code, vendor name, product description and product...