Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:43:36+00:00 2026-06-17T08:43:36+00:00

def open_marks(): N = (int(input(‘how many students? ‘))* 5) students = [] for line

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:43:37+00:00Added an answer on June 17, 2026 at 8:43 am

    You know that you can pass arguments to your functions right? The five open_marks[1-5] functions can be generalized as such:

    def open_marks_for_student(n):
        students = open_marks()
        return students[n-1::5]
    

    If you now pass e.g. 3 to the function, you’ll get the same result as from your old open_marks3 function:

    open_marks_for_student(3)
    

    The same principle can be applied to the count_ranges function, just write a generalized function and pass the marks as an argument:

    def count_ranges(marks):
        range_counts = [0] * 12
        for num in marks[:]: #change number to select number of draws
            which_range=int(num//5)
            range_counts[which_range] = range_counts1[which_range] + 1
        return range_counts
    

    Now use it like this:

    def get_range_counts():
        range_counts = []
        student_nums = [1,2,3,4,5] #or range(1,6)
        for n in student_nums:
            marks = open_marks_for_student(n)
            range_counts.append(count_ranges(marks)) #create histogram, append to list
        return range_counts
    

    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 marks in the loop in count_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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm confused about this scope behavior: class Bar: def __init__(self): for fn in [open,openW,remove,mkdir,exists,isdir,listdir]:
Let's say I have a method like: def open(self, opt): if opt == True:
def file_open(filename): fo=open(filename,'r') #fo.seek(5) fo.read(3) fo.close() file_open(file_ro.py) I expect above program to return first
cat file_ro.py import sys def file_open(filename): fo=open(filename,'r') fo.seek(7) read_data=fo.read(3) fo.close() print read_data file_open(file.py) But
The standard library open function works both as a function: f = open('file.txt') print(type(f))
I want to extend Image class in PIL. #module Image def open(file): ... class
I have a function like this: import csv def total(): with open('test.csv', 'r') as
def download_if_dne(href, filename): if os.path.isfile(filename): # print 'already downloaded:', href return False else: if
def self.jq_column_models COLUMN_NAME.collect {|x| {:name => x.to_s, :width => 80, :format => 'integer' if
def On_Instrumentation_StartAnimation(): Syntax : On_Instrumentation_StartAnimation() Purpose : Fired if the animation is started Parameters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.