Question

Problem 1 Recall that when the built-in function open() is called to open a file for reading, but it doesnt exist, an except
Problem 3 A radar speed gun is a device used in law-enforcement to measure the speed of moving vehicles in miles per hour. Th


def safeFloat (x): try: float (x) return True except ValueError: return false
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def safeOpen(filename):

   try:
       file = open(filename)
   except:
       file = None

   return file

def safeFloat(x):

   try:
       float(x)
       return True

   except ValueError:
       return False


def averageSpeed():

   filename = input("Enter file name: ")
   file = safeOpen(filename)
   if file == None:
       print("File not found. Please try again")

       filename = input("Enter file name: ")
       file = safeOpen(filename)
       if file == None:
           print("File not found. Yet another human error. Goodbye")
           return None


   content = file.readlines()
   avg_speed = []
   for i in content:
       a = i.split()
       for j in a:
           if safeFloat(j) and float(j) > 2.0:
               avg_speed.append(float(j))

   print("Average speed is " + str(round(sum(avg_speed)/len(avg_speed),2)) + " miles per hour")

averageSpeed()

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
  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

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

  • I need help with this assignment in C++, please! *** The instructions and programming style detai...

    I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...

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
ADVERTISEMENT