Question

In: Computer Science

Python program: Write a program that reads a text file named test_scores.txt to read the name...

Python program:

Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student.
Expected Output:
['John', '25', '26', '27']
['Michael', '24', '28', '29']
['Adelle', '23', '24', '20']
[['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']]
Class average for test 1 is: 24.0
Average for student John is 26.00
Average for student Michael is 27.00
Average for student Adelle is 22.33

Solutions

Expert Solution

# function to read file to
# get marks of student
def test_score(filename):
    content = []
    avg=0
    name=[]
    average=[]
    count=0
    test1=0

    # open file
    f=open(filename,"r")

    # read each lines
    for line in f:
        count=count+1
        # content of line
        content=line
        # convert string to integers and stores in array 
        numbers = [int(word) for word in content.split() if word.isdigit()]

        # get the test1 data for each student and add to test1
        test1=test1+numbers[0]
        
        # find current student average score
        avg=sum(numbers)/len(numbers)

        # get the name of each student
        # and store inside list
        data= content.split()
        name.append(data[0])

        # store the average of three subjects
        average.append(avg)

    # display the result
    print("Class average for test 1 is: ",test1/count)
    for i in range(count):
        print("Average for student ",name[i], " is ","%0.2f" % round(average[i],2))
        
        
# main function
def main():
    # call function tet_score to
    # get the targeted result
    test_score("test_scores.txt")
    
    
# driver code
if __name__ == "__main__":
    main()

___________________________________________________________________

John 25 26 27
Michael 24 28 29
Adelle 23 24 20

___________________________________________________________________

Class average for test 1 is:  24.0
Average for student  John  is  26.00
Average for student  Michael  is  27.00
Average for student  Adelle  is  22.33

___________________________________________________________________


Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.


Related Solutions

Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Write a python program that does the following: Prompt for a file name of text words....
Write a python program that does the following: Prompt for a file name of text words. Words can be on many lines with multiple words per line. Read the file and convert the words to a list. Call a function you created called list_to_once_words(), that takes a list as an argument and returns a list that contains only words that occurred once in the file. Print the results of the function with an appropriate description. Think about everything you must...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT