Question

Write a PYTHON program that reads a file (prompt user for the input file name) containing...

Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file:
1   0.5
2   0.5
3   0.5
4   0.5

The output should be:
   The averages are 2.50 and 0.5.

a)   Your code with comments
b)   A screenshot of the execution

Version 3.7.2

0 0
Add a comment Improve this question Transcribed image text
Answer #1
filename = input('Enter file name: ')

with open(filename, 'r') as f:
    total1, total2, count = 0, 0, 0
    for line in f:
        words = line.strip().split()
        total1 += float(words[0].strip())
        total2 += float(words[1].strip())
        count += 1
    print("Averages are {} and {}".format(total1/count, total2/count))

xxixinput.txt Enter Iile name Avezages are 2.5 and o5 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Write a PYTHON program that reads a file (prompt user for the input file name) containing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Error: Writing a program to complete the following: Prompt the user for the name of...

    Python Error: Writing a program to complete the following: Prompt the user for the name of an input file. Open that file for input. (reading) Read the file using a "for line in a file" loop For each line,  o print the line and compute and print the average word length for that line. Close the input file Sample output: MY CODE IS WRONG AND NEED HELP CORRECTING IT!!! ------------------------------------------------------------------------------------------------------------------------ DESIRED OUTPUT: Enter input file name: Seasons.txt Thirty days hath September...

  • Task 1: Write a Python program that takes as input from the user the name of a file containing po...

    python code: Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • (Count the occurrences of each keyword) Write a python program that reads in a Python source...

    (Count the occurrences of each keyword) Write a python program that reads in a Python source code file and counts the occurrence of each keyword in the file. Your program should prompt the user to enter the Python source code filename.

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • Python 3. Date Printer Write a program that reads a string from the user containing a...

    Python 3. Date Printer Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print the date in the format March 12, 2018 3. Date Printer Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print the date in the format March 12, 2018

  • In Python 3, Write a program that reads a set of floating-point values. Ask the user...

    In Python 3, Write a program that reads a set of floating-point values. Ask the user to enter the values, then print: The number of values entered. The smallest of the values The largest of the values. The average of the values. The range, that is the difference between the smallest and largest values. If no values were entered, the program should display “No values were entered” The program execution should look like (note: the bold values are sample user...

  • Compose a program that reads in lines from standard input with each line containing a name...

    Compose a program that reads in lines from standard input with each line containing a name and two integers and then uses writef() to write a table with a column of the names, the integers, and the result of dividing the first by the second, accurate to three decimal places. You could use a program like this to tabulate batting averages for baseball players or grades for students. PLEASE BE IN PYTHON :) ….

  • .Write a program to prompt the user for a file name and a linenumber...

    Write a program to prompt the user for a file name and a line number and then print only that line from the file. Consider possible difficulties and include code to address them. [Hints: it is important to clearly define what you think “line” means. Also, there are a number of errors to consider trapping.]

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT