How to not hardcode function in this example

2024/11/19 2:31:07

The following links contain 2 csv files that the function should pass through grades_1e_2a grades_2e_4a

However my function is only able to pass the 2nd linked file, as it is hardcoded to range(4,8). output: [91.5, 73.5, 81.5, 91.5] The input file will start at the 4th element but may not necessarily end at the 8th element.

def class_avg(open_file):'''(file) -> list of floatReturn a list of assignment averages for the entire class given the openclass file. The returned list should contain assignment averages in theorder listed in the given file.  For example, if there are 3 assignmentsper student, the returned list should 3 floats representing the 3 averages.'''marks=[[],[],[],[]]avgs = []for line in open_file:grades_list = line.strip().split(',')for idx,i in enumerate(range(4,8)):marks[idx].append(float(grades_list[i]))for mark in marks:avgs.append(float(sum(mark)/(len(mark))))return avgs

How do I fix this so that my code will be able to read both files, or any file? I have already opened the file and iterated past the first line with file.readline() in a previous function on the same file. Thanks for everyone's help in advance.

Updated progress: https://gyazo.com/064dd0d695e3a3e1b4259a25d1b0b1a0

Answer

As both sets of your data start the same place the following works for idx,i in enumerate(range(4,len(grades_list))):

This should fulfill all requirements that Im aware of up to this point

def class_avg(open_file):'''(file) -> list of floatReturn a list of assignment averages for the entire class given the openclass file. The returned list should contain assignment averages in theorder listed in the given file.  For example, if there are 3 assignmentsper student, the returned list should 3 floats representing the 3 averages.'''marks = Noneavgs = []for line in open_file:grades_list = line.strip().split(',')if marks is None:marks = []for i in range(len(grades_list) -4):marks.append([])for idx,i in enumerate(range(4,len(grades_list))):marks[idx].append(int(grades_list[i]))for mark in marks:avgs.append(float(sum(mark)/(len(mark))))return avgs
https://en.xdnf.cn/q/120004.html

Related Q&A

How to group array based on the same values

Please, confused with array in python. I want to group array based on the same values.Code:enter image description hereid_disease = [penyakit_tepung,hawar_daun] for id_disease in id_disease:qres = acac…

latest video by youtube api

In order to learn this api I am trying to create a bot.one of the things this bot does is to first comment when a channel uploads a video.On some channels it works however on some channels it doesnt wo…

Function to switch between two frames in tkinter

Im looking through the code at passing-functions-parameters-tkinter-using-lambda, and needed a tad more functionality inside his class PageOne(tk.Frame). Instead of using lambda commands below (as he …

Get values from a tuple in a list in Python

x = Bookshop() x.orders = [ [1, ("5464", 4, 9.99), ("8274",18,12.99), ("9744", 9, 44.95)], [2, ("5464", 9, 9.99), ("9744", 9, 44.95)], [3, ("5464&…

Last Digit of the Sum of Fibonacci Numbers

I am trying to find the last digit of sum of Fibonacci Series. I calculate the sum as F(n+2) - 1. The below code is working fine but it is slow for large numbers (e.g 99999). How can I optimize this?n…

Django websites not loading

I have two Django websites on one server using Apache with mod_wsgi on Windows 10. For some reason the Django websites dont load, however, I have a normal website that does. Ive had it work in the past…

list manipulation and recursion

I have a mansory-grid in a pdf-page. The grid is choosen randomly, so i do not know how much upright cells or cross cells I have to fill. In my list I have all images that I want to proceed, each marke…

Getting TypeError: int object is not callable

Getting TypeError: int object is not callable. What am i doing wrong as i just want to add 10 to the z variableprint ("Hello World")x=int(input("Enter X")) y=int(input("Enter Y…

How to create a loop from 1-9 and from a-z?

I currently use:for i in range(1,10):print iWhich prints the digits 1 to 9. But I want to add a-z to the mix. How can I combine them?

Need Python to accept upper and lower case input

I want my Python code to accept both uppercase and lowercase input.Ive tried casefold, but with no luck. Any help?advice ="" while advice !=("Yes"):print("Would you like some…