In: Computer Science
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, 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.
**I really need something to compare my work to and am struggling with adding the "100 or 0" part.
code in python (code to copy)
from pathlib import Path
# Prints The maximum and minimum value
def printMinMax(list):
if(len(list)==0):
print("Min: List is empty!")
print("Max: List is empty!")
else:
min_num=list[0]
max_num=list[0]
for item in list:
min_num=min(min_num, item)
max_num=max(max_num, item)
print("Min: ",min_num)
print("Max: ",max_num)
# f any values were 100. No output if no values were 100.
# If any values were 0. No output if no values were 0
def hundredAndZero(list):
count100 = 0
count0 = 0
for item in list:
if(item==0):
count0 = count0 + 1
if(item==100):
count100 = count100 + 1
if(count0!=0):
print(count0, " value(s) were 0")
if(count100!=0):
print(count100, " value(s) were 100")
# The average – display with 4 places after the decimal point
def printAverage(list):
if(len(list)==0):
print("Average: List is empty!")
else:
sum=0
for item in list:
sum = sum + item
average = float(sum)/len(list)
print("Average: %.4f"%average)
# The total number of values greater than or equal to val
def greaterThanEqualTo(list, val):
count=0
for item in list:
if(item>=val):
count = count+1
return count
# The total number of values less than val
def lessThan(list, val):
count=0
for item in list:
if(item<val):
count = count+1
return count
# The value closest to val
# If goOver is true we can cross value and use abs
# if goOver is false then we find strictly less than value
def closestTo(list, value, goOver):
closest = -1
min_diff = 0
for i in range(len(list)):
if(not goOver and list[i]>value):
continue
if(closest==-1):
closest=i
min_diff = abs(list[i]-value)
else:
if(min_diff>abs(list[i]-value)):
closest=i
min_diff=abs(list[i]-value)
if(closest==-1):
print("Closest to %d: List is empty!"%value)
print("Closest to %d: %d"%(value, list[closest]))
def main():
filename = input("Enter file name: ")
f = Path(filename)
if not f.is_file():
print("File does not exist!")
else:
myfile = open(f, "r")
nums=[]
for line in myfile:
for x in line.split():
nums.append(int(x))
printMinMax(nums)
hundredAndZero(nums)
printAverage(nums)
# The total number of values
print("Total number of values: ", len(nums))
print("Number of values greater than equal to 75: ",greaterThanEqualTo(nums, 75))
print("Number of values less than 75: ",lessThan(nums, 75))
closestTo(nums, 75, True)
closestTo(nums, 75, False)
main()
code screenshot
contents in input.txt
output screenshot 1
output screenshot 2