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]
3 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]3 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
_______ imaging technology defines locations in the brain where neurons are especially active using safe radioactive isotopes to
Radda [10]

Answer:

Positron Emission Tomography (PET)

Explanation:

4 0
3 years ago
Read 2 more answers
A new laptop was delivered to your home and left on the porch while you were at work. It is very cold outside, and you don't kno
kifflom [539]

Answer:

Let the computer warm up to room temperature before turning it on.

Explanation:

The laptop has been shipped to the user's home when he were at work and found on the deck. Outside it is really cool and he don't remember how long it has been there. Then, the user open his laptop and want to know whether it is functioning properly. Instead, let the machine warm up while switching it on to room temperature.

When a computer is still in a cold climate, they have to let it heat up to room temperature apx. 6-24 hours before switching it on. Otherwise, its heat the elements generate could cause water moisture within the laptop. That water could then harm elements of the system.

7 0
3 years ago
Amy wants to insert clip art in her document, and she wants it to be in gif file format. How can she look for only clip art in a
Nikitich [7]
Go to the motion pics or gifs

5 0
3 years ago
The Apple II is an IBM-compatible PC "clone.
lutik1710 [3]
Is this supposed to be a true or false question?
4 0
3 years ago
Before u can open and edit a file, you need to zip it, T/F
koban [17]

Answer

False, it is not necessary that you need to zip a file always when you are opening and editing a file

<u><em>PLS MARK BRAINLIEST</em></u>

5 0
3 years ago
Other questions:
  • Exposing employee and customer personal data to an untrusted environment is an example of:
    9·1 answer
  • A circuit has a resistance of 300 ohm and a current of 0.024 A. What is the voltage in the circuit?
    12·1 answer
  • A DSLR camera is made up of two parts. They are
    13·2 answers
  • Intro to cs 3.7 edhesive g=float(input("Enter your English test grade:")) if(g&lt;=64): print("F") if (g&gt;=65 and g&lt;69): pr
    11·1 answer
  • Access Control can be defined as putting controls (or countermeasures) in place to help achieve information security goals. Whic
    13·2 answers
  • Jody should select the
    5·1 answer
  • Describe how a web browser and web server work together to send a web page to a user
    8·1 answer
  • Var1 = 1<br> var2 = 2<br> var3 = "3"<br> print(var1 + var2 + var3)
    5·1 answer
  • Which is an example of a demand account
    10·1 answer
  • Live footage refers to:
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!