Write a program that reads an arbitrary number of real values from a user- specified input data file, rounds the values to the nearest integer, and writes the integers out to a user-specified output file. Make sure that the input file exists, and if not, tell the user and ask for another input file. If the output file exists, ask the user whether or not to delete it. If not, prompt for a different output file name.

Short Answer

Expert verified
To create a program that reads an arbitrary number of real values from a user-specified input file, rounds the values to the nearest integer, and writes the integers to a user-specified output file, follow these steps: 1. Prompt the user for input and output file names. 2. Check if the input file exists. If not, prompt for another input file name. 3. Check if the output file exists. If yes, ask the user whether to delete it or provide a different name. 4. Read real values from the input file and store them in a list. 5. Round the real values to the nearest integer using Python's round() function. 6. Write the rounded integers to the output file, with each integer on a new line.

Step by step solution

01

1. Get input file name from the user

The program should prompt the user for the name of the input file. You can use the `input()` function to get the user's input. Example: ```python input_file_name = input("Enter the name of the input file: ") ```
02

2. Check if the input file exists

Use Python's `os.path` module to determine if the input file exists. If not, prompt the user for another input file. Example: ```python import os while not os.path.exists(input_file_name): print("The file does not exist.") input_file_name = input("Enter another input file name: ") ```
03

3. Get output file name from the user

Prompt the user for the name of the output file, using the `input()` function. Example: ```python output_file_name = input("Enter the name of the output file: ") ```
04

4. Check if the output file exists

Use Python's `os.path` module to determine if the output file exists. If it does, ask the user whether or not to delete it. If the user decides not to delete it, prompt for a different output file name. Example: ```python while os.path.exists(output_file_name): response = input("The output file already exists. Do you want to delete it? (Yes/No): ") if response.lower() == "yes": os.remove(output_file_name) else: output_file_name = input("Enter a different output file name: ") ```
05

5. Read real values from the input file

Using a `with` statement, open the input file in read mode and store the real values in a list. Example: ```python with open(input_file_name, "r") as input_file: real_values = [float(line.strip()) for line in input_file.readlines()] ```
06

6. Round the real values to the nearest integer

Use Python's `round()` function to round the real values to the nearest integer. You can achieve this using a list comprehension. Example: ```python rounded_integers = [round(value) for value in real_values] ```
07

7. Write the rounded integers to the output file

Use a `with` statement to open the output file in write mode, and write the rounded integers to the file. Ensure that each integer is written on a new line. Example: ```python with open(output_file_name, "w") as output_file: for integer in rounded_integers: output_file.write(str(integer) + '\n') ``` After following these steps, you should have a program that successfully reads an arbitrary number of real values from a user-specified input file, rounds the values to the nearest integer, and writes the integers to a user-specified output file.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Table of Sines and Cosines. Write a program to generate a table containing the sine and cosine of \(\theta\) for \(\theta\) between \(0^{\circ}\) and \(90^{\circ}\), in \(1^{\circ}\) increments. The program should properly label each of the columns in the table.

Write a MATLAB program that reads in a time in seconds since the start of the day (this value will be somewhere between \(0.0\) and \(86400.0\) ), and prints a character string containing time in the form HH:MM: SS using the 24-hour clock convention. Use the proper format converter to ensure that leading zeros are preserved in the MM and ss fields. Also, be sure to check the input number of seconds for validity, and write an appropriate error message if an invalid number is entered.

Suppose that you have a sum of money \(P\) in an interest-bearing account at a local bank ( \(P\) stands for present value). If the bank pays you interest on the money at a rate of \(i\) percent per year and compounds the interest monthly, the amount of money that you will have in the bank after \(n\) months is given by the equation $$ F=P\left(1+\frac{i}{1200}\right)^{n} $$ where \(F\) is the future value of the account and \(\frac{i}{12}\) is the monthly percentage interest rate (the extra factor of 100 in the denominator converts the interest rate from percentages to fractional amounts). Write a MATLAB program that will read an initial amount of money \(P\) and an annual interest rate \(i\), and will calculate and wriv out a table showing the future value of the account every month for the next 5 years. The table should be written to an output file called "interest': Be sure to properly label the columns of your table.

Angles are often measured in degrees \(\left({ }^{\circ}\right)\), minutes \(\left({ }^{*}\right)\), and seconds \(\left({ }^{*}\right)\), with 360 degrees in a circle, 60 minutes in a degree, and 60 seconds in a minute. Write a program that reads angles in radians from an input disk file, and converts them into degrees, minutes, and seconds. Test your program by placing the following four angles expressed in radians into an input file, and reading that file into the program: \(0.0,1.0,3.141593,6.0\).

The acceleration due to the Earth's gravity at any height \(h\) above the surface of the Earth is given by the equation $$ g=-G \frac{M}{(R+h)^{2}} $$ where \(G\) is the gravitational constant \(\left(6.672 \times 10^{-11} \mathrm{~N} \mathrm{~m}^{2} / \mathrm{kg}^{2}\right), M\) is the mass of the Earth \(\left(5.98 \times 10^{24} \mathrm{~kg}\right), R\) is the mean radius of the Earth \((6371 \mathrm{~km})\), and \(h\) is the height above the Earth's surface. If \(M\) is measured in \(\mathrm{kg}\) and \(R\) and \(h\) in meters, then the resulting acceleration will be in units of meters per second squared. Write a program to calculate the acceleration due to the Earth's gravity in \(500-\mathrm{km}\) increments at heights from \(0 \mathrm{~km}\) to \(40,000 \mathrm{~km}\) above the surface of the Earth. Print out the results in a table of height versus acceleration with appropriate labels, including the units of the output values. Plot the data as well.

See all solutions

Recommended explanations on Psychology Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free