45.3k views
1 vote
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 "", line 1, in print(open('ghost.txt')) FileNotFoundError: [Errno 2] No such file or directory: 'ghost.txt >>> • safeOpen() >>> inputFile = safeOpen('ghost.txt') >>> print(inputFile) None >>> Problem 3 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.92, 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 78.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.87 miles per hour def safeFloat (x): try: float (x) return True except ValueError: return false

User Ben Mabey
by
4.6k points

1 Answer

6 votes

Answer:

could you shorten it a little?

Step-by-step explanation:

User Danny Brady
by
4.8k points