Problem 1
Recall that when the built-in function
open()
is called to open a file for reading, but it doesn’t exist, an
exception is raised. However, if the file exists, a reference to the opened file object is returned.
Write a function
safeOpen()
that takes one parameter,
filename
— a string giving the pathname of the file
to be opened for reading. When
safeOpen()
is used to open a file, a reference to the opened file object
should be returned if no exception is raised, just like for the
open()
function. If an exception is raised
while trying to open the file,
safeOpen()
should return the value
None
.
For example, assuming the file
ghost.txt
doesn’t exist, the following is correct output:
>>> # open()
>>> print(open('ghost.txt'))
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
print(open('ghost.txt'))
FileNotFoundError: [Errno 2] No such file or directory: 'ghost.txt'
>>>
>>> # safeOpen()
>>> inputFile = safeOpen('ghost.txt')
>>> print(inputFile)
None
>>>
Problem 2
Recall that when the built-in function
float()
is called it returns a floating point number constructed from a
number or string. However, if the string doesn’t represent a valid floating point value, an exception is
raised.
Write a function
safeFloat()
that takes one parameter,
x
— a number or string that needs to be converted
to floating point number. When
safeFloat()
is used to convert a number or string, an equivalent floating
point number is returned if no exception is raised, just like for the
float()
function. If an exception is raised
while trying to convert a number or string,
safeFloat()
should return
0.0
floating point value.
For example, the following is correct output:
>>> # float()
>>> float('abc')
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
float('abc')
ValueError: could not convert string to float: 'abc'
>>>
>>> # safeFloat()
>>> f = safeFloat('abc')
>>> print(f)
0.0
>>>
A radar speed gun is a device used in law-enforcement to measure the speed of moving vehicles in miles
per hour. The measured speeds are supposed to be stored in a file, one number per line, as follows:
65.6
70.2
54.9
Unfortunately, due to an intermittent fault, occasionally multiple numbers are written on a single line as
follows:
73.2 65.6 69.8
Furthermore, occasionally the radar gun outputs a single stray character such as:
67.9z
,
6$4.9
, or
a3.9
, to
illustrate just a few.
Given a file that has radar speed gun readings, write a function
averageSpeed()
to calculate the average of
the numbers in the file. Your code must adhere to the following specifications:
a.
Prompt the user for the name of the input file to process. When the user enters a nonexistent file
name, give the user a second chance. After two wrong entries in a row, quit the program with an
appropriate message.
b. Ignore numbers containing stray characters.
c. Ignore any reading for slow vehicles moving at 2 miles per hour or less.
d. Print the final average to the console.
e. Make use of the functions
safeOpen()
and
safeFloat()
.
For example, the following is correct input/output:
>>> inputFile = open('radar.txt')
>>> content = inputFile.read()
>>> print(content)
35.2
1.8
65.6
67.9z
70.2
73.2 a3.9 65.6 69.8
6$4.9
54.9
>>> inputFile.close()
>>>
>>> averageSpeed()
Enter file name: ghost.txt
File not found. Please try again.
Enter file name: phantom.txt
File not found. Yet another human error. Goodbye.
>>>
>>> averageSpeed()
>>> Enter file name: radar.txt
>>> Average speed is 62.07 miles per hour.
Note: As per HomeworkLib policy , only first question will be answered in case of multiple questions. Hence answering first question only
#function to safe open a file
def safeOpen(filename):
try:
f = open(filename)
return f
except FileNotFoundError:
return None
print(safeOpen("ghost.txt"))
Screen shot:

main.py 1 #function 2 def safeOpen (filename ) : 3 try: f = open(filename) 4 return f except FileNot Found E rror : 6 return None 10 print(safeOpen("ghost.txt" )) None ...Program finished with exit code 0 Press ENTER to exit console.
Problem 1 Recall that when the built-in function open() is called to open a file for...
Problem 1 Recall that when the built-in function open() is called to open a file for reading, but it doesn't exist, an exception is raised. However, if the file exists, a reference to the opened file object is returned. Write a function safeOpen() that takes one parameter, filename - a string giving the pathname of the file to be opened for reading. When safeOpen() is used to open a file, a reference to the opened file object should be returned...
C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything. Your function should be named...
c++ program Notes 1. Your program should use named string constant for the file name to open the file. Do not add any path to the file name because the path on your computer is not likely to exist on the computer the instructor uses to grade the program. Points may be deducted if you don't follow this instruction. 2. Split statements and comments longer than 80 characters into multiple lines and use proper indentations for the split portions. 3....
in python please
PROBLEM : (15 POINTS Use the file 'presidents age, txt You can download this file from D2L (Content' tab) to your lo computer This file contains the age (at the time of their inaguration) of the first 45 presidents (including repeats). Each president's name is separated from his age by a comma Write a function called getAges (). Inside this function, create a dictionary in which every key is the name, and the value is the president's...
Python Coding The script, `eparser.py`, should: 1. Open the file specified as the first argument to the script (see below) 2. Read the file one line at a time (i.e., for line in file--each line will be a separate email address) 3. Print (to the screen) a line that "interprets" the email address as described below 4. When finished, display the total number of email addresses and a count unique domain names Each email address will either be in the...
Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...
CSC110
Lab 6 (ALL CODING IN JAVA)
Problem: A text file contains a paragraph. You are to read the
contents of the file, store the UNIQUEwords and count the
occurrences of each unique word. When the file is completely read,
write the words and the number of occurrences to a text file. The
output should be the words in ALPHABETICAL order along with the
number of times they occur and the number of syllables. Then write
the following statistics to...
I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...
YOU NEED TO MODIFY THE PROJECT INCLUDED AT THE END AND USE THE INCLUDED DRIVER TO TEST YOUR CODE TO MAKE SURE IT WORK AS EXPECTED. Thanks USING PYTHON, complete the template below such that you will provide code to solve the following problem: Please write and/or extend the project5 attached at the end. You will have to do the following for this assignment: Create and complete the methods of the PriceException class. Create the processFile function. Modify the main...