Question

I dont know how to get this code working??? this is my code: # read_running_times.py #...

I dont know how to get this code working???

this is my code:

# read_running_times.py
# Chapter 6.2 Page 258


def main():
# Open the video_times.txt file for reading.
video_file = open('video_times.txt', 'r')

# Initialize an accumulator to 0.0
total = 0.0

# Initialize a variable to keep count of the videos.
count = 0

print('Here are the running times for each video:')

# Get the values from the file and total them.
for line in video_file: # Error 1: use : to end this line
# Convert a line to a float.
run_time = float(line)

# Add 1 to the count variable.
count += 1

# Display the time.
print('Video #', count, ': ', run_time, sep='')

# Add the time to total.
total += run_time # Error 2: add run_time to total

# Close the file
video_file.close()

# Display the total of the running times.
print('The total running time is', total, 'seconds.')


# Call the main function
main()

and this is the error that I get:

Traceback (most recent call last):
File "/Users/muus/Desktop/DebuggingPython/read_running_times.py", line 39, in <module>
main()
File "/Users/muus/Desktop/DebuggingPython/read_running_times.py", line 7, in main
video_file = open('video_times.txt', 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'video_times.txt'


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



# read_running_times.py
# Chapter 6.2 Page 258


def main():
    try:
        # Open the video_times.txt file for reading.
        video_file = open('video_times.txt', 'r')

        # Initialize an accumulator to 0.0
        total = 0.0

        # Initialize a variable to keep count of the videos.
        count = 0

        print('Here are the running times for each video:')

        # Get the values from the file and total them.
        for line in video_file:  # Error 1: use : to end this line
            # Convert a line to a float.
            run_time = float(line)

            # Add 1 to the count variable.
            count += 1

            # Display the time.
            print('Video #', count, ': ', run_time, sep='')

            # Add the time to total.
            total += run_time  # Error 2: add run_time to total

        # Close the file
        video_file.close()

        # Display the total of the running times.
        print('The total running time is', total, 'seconds.')
    except FileNotFoundError:
        print("video_times.txt does not exists.")


# Call the main function
main()
Add a comment
Know the answer?
Add Answer to:
I dont know how to get this code working??? this is my code: # read_running_times.py #...
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
  • Python This is the code we are using: def main (): video_file = open ( 'video_times.txt')...

    Python This is the code we are using: def main (): video_file = open ( 'video_times.txt')    list = [] for line in video_file: run_time = float(line) list.append (run_time) print ('Video #', ':', run_time) print(list) sum = 0 for i in range(len(list)): sum = sum + list[i]    video_file.close main ( ) The following text is read from the video_times.txt file: Video # 1 : 10.0 Video # 2 : 24.5 Video # 3 : 12.2 Video # 4 :...

  • When i run my python code i get the following error... and im not sure how...

    When i run my python code i get the following error... and im not sure how to fix the error, can someone help me? ____________________Hers my Code__________________ import face_recognition import cv2 # Get a reference to your webcam video_capture = cv2.VideoCapture(0) # Load a sample picture of yourself and learn how to recognize it. kyle_image = face_recognition.load_image_file("kyle.jpg") kyle_face_encoding = face_recognition.face_encodings(kyle_image)[0] # Load a second sample picture and learn how to recognize it. ##lauren_image = face_recognition.load_image_file("lauren.jpg") ##lauren_face_encoding = face_recognition.face_encodings(lauren_image)[0] # Load...

  • Python function: This is my code and I don't know how to add the column to...

    Python function: This is my code and I don't know how to add the column to the csv file: def write_with_averages(read,write): with open (read,'r') as f_in: header = f_in.readline() reader=csv.reader(f_in) with open (write,'w') as f_out: writer=csv.writer(f_out) New_data=[] for Name, Test1, Test2, Test3 in reader: Total=(float(Test1)+float(Test2)+float(Test3)) average=Total/3 New_data.append(average) Hint: Skip the first line of the file as it is a header line Hint: All values come in as strings, so convert to floats before mathematical comparisons and calculations. File Format Name,Test1,Test2,Test3...

  • What is wrong with my python code. I am receiving this error: Traceback (most recent call...

    What is wrong with my python code. I am receiving this error: Traceback (most recent call last): File "C:/Users/owner/Documents/numberfive.py", line 7, in if age > 18: TypeError: unorderable types: str() > int() My code is age= input("Enter your age:") if age > 18: print("You can vote.") else: print("You can't vote.") if age > 64: print ("You're a Senior Citizen...you deserve two votes") endofprogram = input("Please hit enter to exit from this program:")

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

  • THIS IS THE CODE I AM TRYING TO RUN code # def main():       #Reading a as...

    THIS IS THE CODE I AM TRYING TO RUN code # def main():       #Reading a as an integer       a = int(input("Enter value for a: "))       #reading b as an interger       b = int(input("Enter value for b: "))       #reading n as an integer       n = int(input("Enter value for n: "))       # displaying if a and b are congruent modulo n, this is True if a and b       #both returns same remainder when divided by n       if a % n == b...

  • Im struggling with my final assignment and I don’t know how to start. The code has...

    Im struggling with my final assignment and I don’t know how to start. The code has to be done in python using tkinter module. I would very appreciate the help thank you QUESTION 1 Write a program that counts the number of names on a list. The program must include a class named FinalProjectlastName with the following attributes (class variables) and functions: Name: FinalProjectLastName Attributes: names: list of strings that stores names count: number of names on the list. Functions:...

  • I am suppose to have my array before the main class but I am getting the...

    I am suppose to have my array before the main class but I am getting the error 7 errors found: File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 60] Error: non-static variable objectArray cannot be referenced from a static context File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 61] Error: non-static variable objectArray cannot be referenced from a static context File: C:\Users\diego\OneDrive\Desktop\school\Spring 2020 classes\How to program java (Late Objects)\PO1\Test_ResidencePolicy.java [line: 67] Error: non-static variable objectArray cannot...

  • Python Programming 4th Edition: Need help writing this program below. I get an error message that...

    Python Programming 4th Edition: Need help writing this program below. I get an error message that says that name is not defined when running the program below. My solution: def main(): filename = "text.txt" contents=f.read() upper_count=0 lower_count=0 digit_count=0 space_count=0 for i in contents: if i.isupper(): upper_count+=1 elif i.islower(): lower_count+=1 elif i.isdigit(): digit_count+=1 elif i.isspace(): space_count+=1 print("Upper case count in the file is",upper_count) print("Lower case count in the file is",lower_count) print("Digit count in the file is",digit_count) print("Space_count in the file is",space_count)...

  • Hello, This is what i wrote: def squareEach(nums): answer = [] for num in nums: answer.append(num*num)...

    Hello, This is what i wrote: def squareEach(nums): answer = [] for num in nums: answer.append(num*num) return answer def sumList(nums): answer = 0 for num in nums: answer += num return answer def toNumbers(nums): new_nums = [] for i in nums: new_nums.append(int(i)) return new_nums def main(): fileName = input("What file are the numbers in? ") sum = 0 with open(fileName, 'r') as infile: for line in infile: nums.append(line) print(nums) nums = toNumbers(line) print(nums) squares = squareEach(nums) print(nums) sum += sumList(squares)...

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