In: Computer Science
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.
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.