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
For electronically deposited funds, such as an employee's direct deposit, when must banks make those funds available to the empl
Kazeer [188]
I'm in computers class and I would say
6 0
3 years ago
10. What is "bandwidth"? (
natali 33 [55]

Answer:

ELECTRONICS

a range of frequencies within a given band, in particular that used for transmitting a signal.

2.

the energy or mental capacity required to deal with a situation.

Explanation:

6 0
3 years ago
Using this statement to answer the following question “FIFA World Cup 2010: (1+9) From Today!” (Ignore Quotation)
Yuliya22 [10]
Free space going around the town states
5 0
2 years ago
I don't understand how to write code for this in Java using nested for loops only. The official question is: write a program tha
Bingel [31]

Answer:

public class Triangle

{

public static void main( String[] args )

{

show( 5 );

}

public static void show( int n )

{

int i,j,k;

for (i = 0; i < n - 1; i++ )

{

for (j = 0; j < i; j++ )

{

System.out.print( " " );

}

for (k = n - i; k > 0; k-- )

{

System.out.print( "* " );

}

System.out.println();

}

for (i = 0; i < n; i++ )

{

for (j = n - i; j > 1; j-- )

{

System.out.print( " " );

}

for (k = 0; k < i + 1; k++ )

{

System.out.print( "* " );

}

System.out.println();

}

4 0
3 years ago
Algorithm that has been coded into something that can be run by a machine.
hram777 [196]
The answer is program
8 0
3 years ago
Other questions:
  • If you define CSS rules on a parent control, the rules will be inherited by all of the children widgets.
    12·1 answer
  • Spreadshet formula to add totals​
    12·1 answer
  • What can use so tha the slides can appear?
    13·1 answer
  • This common technique, employed at the edge of a network, eliminates the need for public IP addresses on a private network while
    8·1 answer
  • How was windows 3 installed on a pc?
    11·1 answer
  • Summarizes statistical data ?
    11·1 answer
  • Turning up the transmit power or utilizing a high gain antenna to reach wireless users from a distance increases your exposure t
    12·1 answer
  • Which of the following actions is an example of "window dressing?" a. Using some of the firm’s cash to reduce long-term debt. b.
    13·1 answer
  • During the ___ phase of a software development project, members broadly define the features that the software should include
    15·1 answer
  • 8. Give regular expressions with alphabet {a, b} for
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!