def open_marks():
N = (int(input('how many students? '))* 5)
students = []
for line in open('marks.txt').readlines():
datafile = (line.strip().split('\t')[0].split(','))
for n in datafile:
students.append(int(n))
students=students[:N]
return students
def open_marks1():
students = open_marks()
students1=students[0::5]#set to return only the first(lowest) marks drawn
return students1
def open_marks2():
students = open_marks()
students2=students[1::5]#set to return only the second marks drawn
return students2
def open_marks3():
students = open_marks()
students3=students[2::5]#set to return only the third marks drawn
return students3
def open_marks4():
students = open_marks()
students4=students[3::5]#set to return only the fourth marks drawn
return students4
def open_marks5():
students = open_marks()
students5=students[4::5]#set to return only the fifth(highest) marks drawn
return students5
def count_ranges_one():
students1 = open_marks1()
print('first number: ',students1)
range_counts1 = [0] * 12
for num in students1[:]:#change number to select number of draws
which_range=int(num//5)
range_counts1[which_range] = range_counts1[which_range] + 1
return range_counts1
def count_ranges_two():
students2 = open_marks2()
print('second number: ',students2)
range_counts2 = [0] * 12
for num in students2[:]:#change number to select number of draws
which_range=int(num//5)
range_counts2[which_range] = range_counts2[which_range] + 1
return range_counts2
def count_ranges_three():
students3 = open_marks3()
print('third number: ',students3)
range_counts3 = [0] * 12
for num in students3[:]:#change number to select number of draws
which_range=int(num//5)
range_counts3[which_range] = range_counts3[which_range] + 1
return range_counts3
def count_ranges_four():
students4 = open_marks4()
print('fourth number: ',students4)
range_counts4 = [0] * 12
for num in students4[:]:#change number to select number of draws
which_range=int(num//5)
range_counts4[which_range] = range_counts4[which_range] + 1
return range_counts4
def count_ranges_five():
students5 = open_marks5()
print('fifth number: ',students5)
range_counts5 = [0] * 12
for num in students5[:]:#change number to select number of draws
which_range=int(num//5)
range_counts5[which_range] = range_counts5[which_range] + 1
return range_counts5
I have a text file with student marks in the format:
7,5,10,25,32
9,15,25,39,18
etc
the above groups the marks according to the position (1,2,3,4, or 5) and then there is a routine to ‘write’ a histogram’ to show the distribution of marks. What I have written is terribly clumsy and repetitive, but I cannot figure out looped functions to deliver the data grouped as marks per question and then the number per range for the histogram. Can someone help me with the logic please.
You know that you can pass arguments to your functions right? The five
open_marks[1-5]functions can be generalized as such:If you now pass e.g. 3 to the function, you’ll get the same result as from your old open_marks3 function:
The same principle can be applied to the
count_rangesfunction, just write a generalized function and pass the marks as an argument:Now use it like this:
There’s still a few optimizations one could make, e.g. opening the marks file only once instead of once per student, or removing the slicing on
marksin the loop incount_ranges(you only need to iterate over a copy if you plan on modifying the original list in the loop), but this should be enough to get you started.