Question

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 : 14.6

Video # 5 : 20.4

Video # 6 : 22.5

Video # 7 : 19.3

Video # 8 : 18.6

Video # 9 : 17.5

Video # 10 : 14.8

Video # 11 : 12.2

How do I change the code so that the total running time and average running time is displayed at the end of this output? We need to sum the times and figure out the average.

The total running time is 186.6 seconds

The average running time is 16.963636363636365

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

#Python program

def main ():
video_file = open ( 'video_times.txt')
list = []
count = 0
sum=0
for line in video_file:
run_time = float(line)
list.append (run_time)
  
for run_time in list:
count+=1
print ('Video #',count, ':', run_time)
sum = sum + run_time
  
print('The total running time is ',sum,' seconds')
print('The average running time is ',sum/count)
  
video_file.close
  
main ( )

#Screenshot

File Edit Format Run Options Window Help def main (): video_file = open(video times.txt) list = [] count = 0 sum=0 for line

Add a comment
Know the answer?
Add Answer to:
Python This is the code we are using: def main (): video_file = open ( 'video_times.txt')...
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
  • I am trying to finish the code below. class DVD: def __init__(self, title, dvd_type, cost): self.title = title self.dvd_type = dvd_type self.cost = cost def setTitle(self, netTitle): self.title = newT...

    I am trying to finish the code below. class DVD: def __init__(self, title, dvd_type, cost): self.title = title self.dvd_type = dvd_type self.cost = cost def setTitle(self, netTitle): self.title = newTitle def getTitle(self): return self.title def getType(self): return self.dvd_type def getCost(self): return self.cost def setCost(self, newCost): self.cost = newCost def setType(self, newdvd_Type): validTypes = ['Game', 'Word', 'Compiler', 'Spreadsheet', 'dBase', 'Presentation'] while True: self.dvd_type = input( 'Please enter the valid type: (Game, Word, Compiler, Spreadsheet, dBase, Presentation)') if self.dvd_type() not in validTypes:...

  • Using a sort method and my previous code (put inside of addHours method if possible), how...

    Using a sort method and my previous code (put inside of addHours method if possible), how would I get the list of each employee's total hours to display in order of least amount of hours to greatest. An explanation for each step would also be great. import random #Create function addHours def addHours(lst): #Display added hours of employees print("\nEmployee# Weekly Hours") print("----------------------------") print(" 1 ",sum(lst[0])) print(" 2 ",sum(lst[1])) print(" 3 ",sum(lst[2])) #Create main function def main(): #Create first empty list...

  • python programming: Can you please add comments to describe in detail what the following code does:...

    python programming: Can you please add comments to describe in detail what the following code does: import os,sys,time sl = [] try:    f = open("shopping2.txt","r")    for line in f:        sl.append(line.strip())    f.close() except:    pass def mainScreen():    os.system('cls') # for linux 'clear'    print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")    print(" SHOPPING LIST ")    print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")    print("\n\nYour list contains",len(sl),"items.\n")    print("Please choose from the following options:\n")    print("(a)dd to the list")    print("(d)elete from the list")    print("(v)iew the...

  • Python Error: Writing a program to complete the following: Prompt the user for the name of...

    Python Error: Writing a program to complete the following: Prompt the user for the name of an input file. Open that file for input. (reading) Read the file using a "for line in a file" loop For each line,  o print the line and compute and print the average word length for that line. Close the input file Sample output: MY CODE IS WRONG AND NEED HELP CORRECTING IT!!! ------------------------------------------------------------------------------------------------------------------------ DESIRED OUTPUT: Enter input file name: Seasons.txt Thirty days hath September...

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

  • Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================")...

    Need help writing this code in python. This what I have so far. def display_menu(): print("======================================================================") print(" Baseball Team Manager") print("MENU OPTIONS") print("1 - Calculate batting average") print("2 - Exit program") print("=====================================================================")    def convert_bat(): option = int(input("Menu option: ")) while option!=2: if option ==1: print("Calculate batting average...") num_at_bats = int(input("Enter official number of at bats: ")) num_hits = int(input("Enter number of hits: ")) average = num_hits/num_at_bats print("batting average: ", average) elif option !=1 and option !=2: print("Not a valid...

  • Please, I need help debuuging my code. Sample output is below MINN = 1 MAXX =...

    Please, I need help debuuging my code. Sample output is below MINN = 1 MAXX = 100 #gets an integer def getValidInt(MINN, MAXX):     message = "Enter an integer between" + str(MINN) + "and" + str(MAXX)+ "(inclusive): "#message to ask the user     num = int(input(message))     while num < MINN or num > MAXX:         print("Invalid choice!")         num = int(input(message))     #return result     return num #counts dupes def twoInARow(numbers):     ans = "No duplicates next to each...

  • convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [...

    convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [ ] if len(nums) == 1: return nums [0] # first find freq freq dict d = {} for num in nums: if num in d: d[num] -= 1 # reverse the sign on the freq for the heap's sake else: d[num] = -1 h = [] from heapq import heappush, heappop for key in di heappush(h, (d[key], key)) res = [] count = 0...

  • This needs to be in python, however, I am having trouble with the code. Is there...

    This needs to be in python, however, I am having trouble with the code. Is there any way to use the code that I already have under the main method? If so, what would be the rest of the code to finish it? #Create main method def main(): #Create empty list list=[] #Display to screen print("Please enter a 3 x 4 array:") #Create loop for rows and have each entered number added to list for i in range(3): list.append([int(x) for...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

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