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
What is the advantages and disadvantages of hardware devices and software devices ?
yulyashka [42]

Answer:

Advantages of hardware:

Physical existence

Multitasking

Speedy

Disadvantages:

Costly as different equipment cost differently

Time consuming

requires space

Privacy issues

Explanation:

Advantages of Software:

Less costly

Improved privacy controls

Requires no space

Disadvantages:

Does not have physical existence

hacking and cyber attacks may steal important information

Regular updates required

3 0
2 years ago
The IT department has rejected the password you submitted for approval to join the network. Your suggestion was "bobbie." Why di
vodomira [7]

4. Although all of them could be true, this one is most acurate :)

8 0
2 years ago
Read 2 more answers
You have been asked to implement a data integrity program to protect data files that need to be electronically downloaded by the
Katyanochek1 [597]

Answer:

SHA-1 is used in this case.

Explanation:

7 0
3 years ago
What is the fifth and final stage in the process of media production
Mama L [17]

edit is the 5th and distribute is the final stage in process of media production


6 0
2 years ago
Please please help me in my question please
NeTakaya
The internet is a vast network that connects computers, cellular devices, and cable together from all over the world. There is wireless internet (Wi-Fi) which doesn’t use lines/cables to connect to devices. Cellular technology provides wireless internet access through cellphones. Finally there is satellite which travels an enormous distance from Earth to a satellite.
3 0
3 years ago
Read 2 more answers
Other questions:
  • At a local burger chain, a customer became extremely angry. Everyone took notice at the person, who was yelling at anyone he cou
    10·2 answers
  • Suppose the information content of a packet is the bit pattern 1110 0110 1001 1101 and an even parity scheme is being used. What
    15·1 answer
  • WHAT 1 + 1 ???? ???????????????????
    15·2 answers
  • Linda subscribes to a cloud service. The service provider hosts the cloud infrastructure and delivers computing resources over t
    10·1 answer
  • Select the two statements below that are true. Press on the info button for
    13·1 answer
  • A computer's hard disk drive holds 8 x 10^10 bytes of information. If Jill buys an extra memory stick that holds 5.1 X 10^8 byte
    10·1 answer
  • "The ability to assign system policies, deploy software, and assign permissions and rights to users of network resources in a ce
    7·1 answer
  • Is a pocket watch consider a computer
    15·1 answer
  • Read the argument below and determine the underlying principle that was used to come to the conclusion presented: It is recommen
    5·1 answer
  • You are trying to connect a new USB device to your computer. You install the driver and then connect the device to an open USB
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!