Question

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 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
Problem 1 Recall that when the built-in function open() is called to open a file for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT