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
Savatey [412]
4 years ago
15

Define a function below called average_num_in_file. The function takes one argument: the name of a file full of numbers. Complet

e the function so that it opens the file and returns the average of the numbers in the file. Here is an example input file from the static test case if you want to test from the interpreter: canned_file.txt
Computers and Technology
1 answer:
Nookie1986 [14]4 years ago
7 0

Answer:

  1. def average_num_in_file(fileName):
  2.    with open(fileName) as file:
  3.        rows = file.readlines()
  4.        sum = 0
  5.        count = 0
  6.        for x in rows:
  7.            sum += float(x)  
  8.            count += 1
  9.        average = sum / count  
  10.    return average  
  11. print(average_num_in_file("cans.txt"))

Explanation:

The solution code is written in Python 3.

Firstly create a function that take one parameter, fileName (Line 1).

Open the file stream and use readlines method to read the data from the file (Line 2-3). Create variable sum and count to tract the total of the number from text files and number of data from the file (Line 5-6). Use a for loop to loop over each row of the read data and add the current value of each row to sum and increment the count by one (Line 7-9).

After the loop, calculate the average (Line 11) and return the result (Line 12).

At last, we can test the function by passing the cans.txt as argument (Line 14).

You might be interested in
Carrie works on a help desk and is assigned a ticket that was automatically generated by a server because of an error. The error
OverLord2011 [107]

I would honestly say that there are a few available tools that Carrie can use, but the best tool is the inbuilt Windows Powershell. As Powershell continues to extend its purpose and usefulness, Microsoft, on the other hand, continues to use Powershell's capability to develop more cmdlets for products like Windows Servers. Everything that can be done in a GUI environment can be done in Powershell. Carrie should be able to use Powershell to run things more efficiently from the command line without stepping a foot on the physical server. She will only need to access the server from her desk remotely, run a few commands, and that is it. Powershell command line is so powerful; it carries with it every troubleshooting pack that you can think about.

5 0
3 years ago
Choose the person responsible for each innovation.
shepuryov [24]

Answer:

John Blankenbaker

Explanation:

5 0
4 years ago
To activate Spelling and Grammar check using the ribbon, navigate first to the _____ tab
ipn [44]
I believe you would click on the Auto Correct Tab
7 0
4 years ago
Read 2 more answers
Tom teaches in a high school. He wishes to sort a spreadsheet containing students' marks in various subjects by descending order
cricket20 [7]

Answer:

Select the data to sort

Explanation:

4 0
3 years ago
Describe artificial intelligence, and contrast the human mind and the computer as information processors
marin [14]
Robots can think better than humans can and they also can’t feel emotion like us. Also, unlike humans, they can predict what will happen next
8 0
4 years ago
Other questions:
  • A software program that allows a programmer to type in code. modern versions usually make it easy to format the code on the scre
    13·1 answer
  • Who defined the term crowdsourcing in a Wired magazine article?
    15·1 answer
  • 1. Write a CFG to generate identifiers (IDs) in the Java Programming Language. An ID in Java is a string of characters consistin
    5·1 answer
  • Taking a picture with a wide aperture creates a shallow depth of field. The subject is up close and in focus and defined while b
    12·2 answers
  • Analyzing the role of elements in audio editing and Video
    7·1 answer
  • Take points for free who are new and enjoye them
    9·2 answers
  • This feature allows you to adjust your view to see the lower and upper part of a document
    15·2 answers
  • Consider the following method substringFound, which is intended to return true if a substring, key, is located at a specific ind
    14·1 answer
  • Which window would show you bindings for local area connection 2?
    5·1 answer
  • Match the definitions to their respective cli hot keys and shortcuts. (not all options are used.)
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!