Question

In: Computer Science

Part 1 Ask for a file name. The file should exist in the same folder as...

Part 1

Ask for a file name. The file should exist in the same folder as the program. If using input() to receive the file name do not give it a path, just give it a name like “number1.txt” or something similar (without the quotes when you enter the name). Then ask for test scores (0-100) until the user enters a specific value, in this case, -1. Write each value to the file as it has been entered. Make sure to close the file once the user has finished entering in the data.

Specifics

You can use input() or FileUtils.selectOpenFile/FileUtils.selectSaveFile for receiving the file name from the user in either part. The FileUtils functions will be discussed in class Wednesday. The file can be downloaded from the examples folder in Blackboard. There may be LOTS of different FileUtils files available in the wild, make sure you get my file from Blackboard. Keep in mind that entering no actual data is legal (after entering a file name you enter -1 right away) and should be considered in part 1 and part 2.

Part 2 (separate program)

Ask for a file name. Don’t let the program crash if you enter the name of a file that does not exist. Detecting this will also be discussed on Wednesday. If the file doesn’t exist gracefully display an error message stating the file doesn’t exist and quit the program. It the file does exist read all the values in the file. You will only need to read the file once, or more to the point, only read the file once. After the program has finished reading all the data, display the following information to the screen: • The minimum value • The maximum value • If any values were 100. No output if no values were 100. • If any values were 0. No output if no values were 0.

• The average – display with 4 places after the decimal point • The total number of values • The total number of values greater than or equal to 75 • The total number of values less than 75

• The value closest to 75 (can be 75, less than 75 or greater than 75). abs will be useful for this value.

• The value closest to 75 WITHOUT going over 75 (can be 75, WILL NOT be greater than 75) If no data exists in the file, write out a simple message indicating that there was no data in the file and nothing else.

Requirements Complete comment section that includes your name, id number, program number and a brief description of the program

and please let me know how to file should exist in the same folder as the program to this assignment.

Solutions

Expert Solution

NOTE -

"The file should exist in the same folder as the program". This line simply means that you should keep the text files that are used as input in the above programs in the same folder on your computer in which the program is. Otherwise, the program will not be able to open the file for reading.

For Example - If you enter "number1.txt" as input in the first program when asked for the filename, then the file "number1.txt" should be in the same folder in which your first program is on your computer. Same is true for 2nd program also.

However, in the first program, since you are going to write to the file, so if the specified file does not exist in the same folder, then your program will create a new file for writing. But same is not true for 2nd program, since we are opening the file for reading in the 2nd program and a file is not created if we are opening it for reading.

PROGRAMMING LANGUAGE - PYTHON

PART 1 -

CODE -

# Name -

# ID -

# Program Number - 1

# Description - This program takes in multiple test scores as input and stores these scores in a text file whose name is also taken as input

# Taking file name as input from the user

filename = input("Enter the file name: ")

# Opening the file in write mode

file = open(filename, "w")

# Loop to run until user enters -1 as input and store the test scores in the file

while(True):

    # Taking a test score as input from the user

    score = int(input("Enter a test score (0-100): "))

    # Exiting the loop if user entered -1 as score

    if score == -1:

        break

    # Otherwise, storing the score in the file if the score is in the range 0-100

    elif score<0 or score>100:

        print("Invalid Input. Test Score should be between 0 and 100!")

    else:

        # Writing the score to the file

        file.write('{} ' .format(score))

# Closing the file

file.close()

SCREENSHOTS -

CODE WITH INPUT/OUTPUT -

OUTPUT TEXT FILE -

PART 2 -

CODE -

# Name -

# ID -

# Program Number - 2

# Description - This program takes in a file name as input containing some values and calculates and prints

#                some basic statistics of the values like average, minimum, maximum, and many more .


# Taking file name as input from the user

filename = input("Enter the file name: ")

# Opening the file in write mode

try:

    file = open(filename, "r")

    # Reading the whole file as a single string

    infile = file.read()

    # Closing the file

    file.close()

    # Displaying message if the file contains nothing

    if(infile == ""):

        print("There is no data in the file")

    else:

        # Splitting the string read by space

        values = infile.split(" ")

        # Initializing variables needed to find the required statistics for the values in the file

        min_val = 99999

        max_val = -99999

        count_val_100 = 0

        count_val_0 = 0

        sum_val = 0

        num_values = 0

        num_val_grtr_75 = 0

        num_val_less_75 = 0

        val_clos_75 = 0

        diff = 99999

        diff_less = 9999

        val_clos_less_75 = 0

        # Iterating over the values

        for value in values:

            # Converting the string form of the value to integer

            value = int(value)

            # Finding the various required stats about the values in the file

            if value > max_val:

                max_val = value

            if value < min_val:

                min_val = value

            if value == 100:

                count_val_100 += 1

            elif value == 0:

                count_val_0 += 1

            if value >= 75:

                num_val_grtr_75 += 1

            elif value < 75:

                num_val_less_75 += 1

            if abs(75 - value) < diff:

                val_clos_75 = value

                diff = abs(75 - value)

            if value <= 75 and (75 - value) < diff_less:

                val_clos_less_75 = value

                diff_less = 75 - value

            sum_val += value

            num_values +=1

        # Finding the average of all the values

        avg_val = sum_val / num_values

        

        # Displaying all the various required information

        print("Minimum value =", min_val)

        print("Maximum value =", max_val)

        if count_val_100 != 0:

            print(count_val_100, "values are equal to 100")

        if count_val_0 != 0:

            print(count_val_0, "values are equal to 0")

        print('Average of all the values = {:.4f}' .format(avg_val))

        print("Total number of values =", num_values)

        print("Total number of values greater than or equal to 75 =", num_val_grtr_75)

        print("Total number of values less than 75 =", num_val_less_75)

        print("Value closest to 75 =", val_clos_75)

        print("Value closest to 75 without going over 75 =", val_clos_less_75)

# Displaying error message and quitting the program if file does not exist

except FileNotFoundError:

    print("File Does not exist! Quitting the Program.")

SCREENSHOTS -

INPUT TEXT FILE -

CODE -

OUTPUT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

PYTHON Part 2 (separate program) Ask for a file name. Don’t let the program crash if...
PYTHON Part 2 (separate program) Ask for a file name. Don’t let the program crash if you enter the name of a file that does not exist. Detecting this will also be discussed on Wednesday. If the file doesn’t exist gracefully display an error message stating the file doesn’t exist and quit the program. It the file does exist read all the values in the file. You will only need to read the file once, or more to the point,...
Script 3: Ask the user for a file's name If the file exists, ask them if...
Script 3: Ask the user for a file's name If the file exists, ask them if they would like to (C)opy, (M)ove, or (D)elete it by choosing C, M, or D If the user chooses C, ask for the destination directory and move it there If the user chooses M, ask for the destination directory and move it there If the user chooses D, delete the file. Ensure that the user enters only C, M, or D, warning them about...
C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming...
C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming read and make a while loop that A) Reads in numbers B) Sums them up. C) Counts how many there are. D) Prints "With 10 numbers, the average is blank.." every 10 numbers. So with every 10 numbers read in print the average. 3. After the loop calculate average of all numbers and print it. So for example once the file is open the...
File IO Java question • Ask the user to specify the file name to open for...
File IO Java question • Ask the user to specify the file name to open for reading • Get the number of data M (M<= N) he wants to read from file • Read M numbers from the file and store them in an array • Compute the average and display the numbers and average.
Ask the user for their name Create a file called "daily.dat" within the home directory Ask...
Ask the user for their name Create a file called "daily.dat" within the home directory Ask the user for a line of text that the system should save to the file Add to the file the line of text entered by the user Ask the user for a second line of text that the system should save to the file Append at the end of the file the line of text entered by the user Create a copy of the...
JAVA There is a folder named Recursive folder at the same location as these below files,...
JAVA There is a folder named Recursive folder at the same location as these below files, which contains files and folders. I have read the data of the Recursive folder and stored in a variable. Now they are required to be encrypted and decrypted. You may copy past the files to three different files and see the output. I am confused on how to write code for encrypt() and decrypt() The names of files and folders are stored in one...
Task 2.5: Write a script that will ask the user for to input a file name...
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created 1. Open a new file script creafile.sh using vi editor # vi creafile.sh 2. Type the following lines #!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created” 3. Add the execute permission 4. Run the script #./creafile.sh 5. Enter...
c++ In this program ask the user what name they prefer for their file, and make...
c++ In this program ask the user what name they prefer for their file, and make a file, the file name should za file. Get a number from the user and save it into the create file. should be more than 20 number in any order. print out all 20 numbers in a sorted array by using for loop, last print out its total and average. At last create a function in which you will call it from main and...
Using the given file, ask the user for a name, phone number, and email. Display the...
Using the given file, ask the user for a name, phone number, and email. Display the required information. These are the Files that I made: import java.util.Scanner; public class Demo5 { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.println("New number creation tool"); System.out.println("Enter name"); String name = keyboard.nextLine(); System.out.println("Enter phone number"); String phoneNumber = keyboard.nextLine(); System.out.println("Enter email"); String email = keyboard.nextLine(); Phone test1 = new SmartPhone(name, phoneNumber, email); System.out.print(test1); System.out.println("Telephone neighbor: " + ((SmartPhone) test1).getTeleponeNeighbor()); }...
Your Application should ask the user to enter their name and their salary.
Create a C# Console Application, name the solution Homework 6 and the project TaxRate.Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT