1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
olganol [36]
4 years ago
8

(Process scores in a text file) Suppose that a text file contains an unspecified number of scores. Write a program that reads th

e scores from the file and displays their total and average. Scores are separated by blanks. Your program should prompt the user to enter a filename. Here is a sample run:

Computers and Technology
1 answer:
lisov135 [29]4 years ago
7 0

Answer:

Here is the Python program:

def scores(file):  # method scores that takes a file name as parameter and returns the sum and average of scores in a file

   with open(file, 'r') as infile:  # open the file in read mode

       lines = [score.split() for score in infile]  # split the scores into a list

       print("The scores are:",lines)  #print the scores

       for line in lines:  # loops through each score

           total= sum(int(score) for score in line)  # adds the scores

           average =total/len(line) # computes average by taking sum of scores and dividing by number of scores in file

       print("The sum is:", total)  #prints the sum of scores

       print("The average is:", "{:.2f}".format(average))   #prints the average

filename = input("Enter name of the file: ")  #prompts user to enter name of file

scores(filename) #calls scores method by passing the file name to it in order to compute sum and average of file contents i.e. scores

Explanation:

It is assumed that the scores in the file are separated by a blank space.

The scores() method takes a file name as parameter. Then it opens that input file in read mode using object infile.

split() method is used to split the scores in a file into a list. Suppose the scores are 1 2 3 4 5 6 . So after the split, they become ['1', '2', '3', '4', '5', '6']  

The loop iterates through each score in the file, splits them into a list and stores this list in lines. The next print statement prints these scores in a list.

The second loop for line in lines iterates through each score of the list and the statements: total= sum(int(score) for score in line)  and average =total/len(line) computes the total and average of scores.

total= sum(int(score) for score in line)  statement works as follows:

for loop iterates through each element of list i.e. each score

int() converts that string element into integer.

sum() method adds the integers to compute their total.

So if we have  ['1', '2', '3', '4', '5', '6']  each element i.e. 1,2,3,4,5,6 is converted to integer by int() and then added together by sum method. So this becomes 1+2+3+4+5+6 = 21. This result is stored in total. Hence

total = 21.

average = total/len(line) works as follows:

The computed sum of scores stored in total is divided by the number of scores. The number of scores is computed by using len() method which returns the length of the line list. So len() returns 6. Hence

average = total/len(line)

              = 21 / 6

average = 3.5

The next two print statement prints the value of sum and average and "{:.2f}".format(average)) prints the value of average up to 2 decimal places.

The screenshot of the program along with its output is attached.

You might be interested in
HELPPPPPPPPP MEEEEE :):):):):)
Pepsi [2]
I believe it’s b. Link text
7 0
3 years ago
The retention of encoded information over time refers to
Crank

It refers to Storage

7 0
2 years ago
Suppose that you need to access a data file named "students.txt" to hold student names and GPAs. If the user needs to look at th
pogonyaev

Answer:

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

Explanation:

In the computing world, a command is an instruction to a computer program to execute a particular task. These instructions might be issued via a command-line interface, such as a shell program, or as a input to a network service as a component of a network protocol, or can be instructed as an event in a graphical user interface activated by the respective user selecting an option in a menu.

the f = open('students.txt', 'r') command line would be used to open a file named "students.txt", There are also various other types of commands for executing different task, for example:

1. ASSOC for Fix File Associations

2. FC for File Compare

3. IPCONFIG for IP Configuration

4. NETSTAT for Network Statistics

5. PING for Send Test Packets

6. TRACERT for Trace Route

7. POWERCFG for Power Configuration

e.t.c.

5 0
3 years ago
Write measure five safety measures can be taken to stay safe in social media​
alexdok [17]

Answer:

  1. <u>Use a strong password</u>.
  2. <u>Use a different password for each of your social media accounts</u>.
  3. <u>Set up your security answers. </u>
  4. <u>If you have social media apps on your phone, be sure to password protect your device.</u>
  5. <u>Be selective with friend requests. </u>

5 0
3 years ago
Read 2 more answers
Once a virus has been removed by your anti-virus program, all traces of it are gone from your computer.
pychu [463]
Im pretty sure that is false
5 0
3 years ago
Other questions:
  • A ____ paragraph places the first character of a text line near the left border of a placeholder.
    14·1 answer
  • The integer variables first and last have been declared and assigned values (with last &gt;= first). Allocate an integer array t
    10·1 answer
  • There are several vehicles in a parking lot. Some of them are motorcycles
    8·2 answers
  • Does the Main Content (MC) of a web page include searchboxes?
    10·1 answer
  • When doing black and white photography, which file format should you use if possible? JPEG TIFF PNG RAW
    11·2 answers
  • A common approach of networking multiple computers as opposed to a single computer is? called:
    14·1 answer
  • One day you tap your smartphone screen to turn it on, and nothing happens. It appears to be turned off and will not turn on. Wha
    12·2 answers
  • What is the operation of mouse in which it is moved while holding left button.​
    11·2 answers
  • About twice a day my Chromebook blacks out. why does it do that?
    8·2 answers
  • How can you apply multimedia for study explain​
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!