how could I make the function count?.. its a test marked out of 5, so if the user enters 1,1,2,2,4,5,
then the output= 2 people got 1 mark, 2 people got 2 marks and 0 got 3 marks and so on?... i dnt knw if my code is even right.. plz help

def mark():
    marks=input("please enter the marks: ")
    finish=raw_input("have you finished yes/no: ")
    if:
        finish=no
        mark()
    break
    elif:
print " student(s) got 0 marks
 student(s) got 1 marks
 student(s) got 2 marks
 student(s) got 3 marks
 student(s) got 4 marks
 student(s) got 5 marks"

Dani AI

Generated

Common problems in the thread: using assignment instead of comparison (= vs ==), relying on recursion to collect repeated input, not validating user input, and resetting counters each time the function is called. correctly pointed out the comparison issue, and showed a simple counter example but that example resets state on each call. A robust solution uses a loop with a sentinel to stop, validates entries (0–5), keeps counts in a fixed-size container, and either returns the counts or prints them once at the end.

A compact, practical approach (Python 3) is to keep a list of six counters and loop until the lecturer types a sentinel like done or an empty line. Validate with int() inside a try/except, reject out-of-range values, and increment counts[mark]. Return the list so the caller can save or reprint results; print only after input finishes.

def testMarks():
    counts = [0] * 6
    while True:
        s = input("mark 0-5 (or 'done' to finish): ").strip()
        if s.lower() in ('', 'done'):
            break
        try:
            m = int(s)
        except ValueError:
            print("Please enter an integer 0..5.")
            continue
        if 0 <= m <= 5:
            counts[m] += 1
        else:
            print("Out of range; enter 0..5.")
    for mark, n in enumerate(counts):
        print(f"{n} student(s) got {mark} marks")
    return counts

Notes and alternatives: for Python 2 replace input with raw_input and adjust print syntax (see raw_input docs and input docs). If you want to parse a single comma-separated line instead of interactive prompts, split the line and use collections.Counter for a concise solution (collections.Counter).

Recommended Answers

All 4 Replies

If you want to print it out, then it's best to print out how many people got a certain amount of marks before starting the function again. Then you have no way of retrieving the data if you write over it. Also, you need:
if finish == no:

instead of

if:
finish = no

I'm sorry if this doesn't help but I don't get what you mean by making the function 'count' and how the user enters stuff. Could you please explain better, and I could help you further.

Thanks
Mark

If you want to print it out, then it's best to print out how many people got a certain amount of marks before starting the function again. Then you have no way of retrieving the data if you write over it. Also, you need:
if finish == no:

instead of

if:
finish = no

I'm sorry if this doesn't help but I don't get what you mean by making the function 'count' and how the user enters stuff. Could you please explain better, and I could help you further.

Thanks
Mark

well basically the usr has to enter the marks the students got, then the function should ask them if they have finished yet. If they have then it should output the numbr of students who have got the certain marjs such if the user enters 4,4 then the output should 0 students got 1 mark, 0 got 2 marks and so on but 2 students got 4 marks... the maximum mark is 5...

i dnt knw if my code is even right.. plz help

No is far from right,this you should know if you try to run it.

Look at this one more time,to you think this will run?

if:
    finish=no
print " student(s) got 0 marks
 student(s) got 1 marks
 student(s) got 2 marks
 student(s) got 3 marks
 student(s) got 4 marks
 student(s) got 5 marks"

And this with singel quote will no work """ my multiline text """ .

Just to show one way to count.

def mark():    
    m1,m2 = 0,0  
    for i in range(5):  #just for test ask 5 times,you can use a while loop and breake out if no
        marks = raw_input("please enter the marks: ")        
        if marks == '1':            
            m1 += 1  #a counting method        
        elif marks == '2':            
            m2 += 1           
    print '%s student got:1' %  (m1)
    print '%s student got:2' %  (m2)
mark() 

'''Out-->
please enter the marks: 1
please enter the marks: 1
please enter the marks: 1
please enter the marks: 2
please enter the marks: 2
3 student got:1
2 student got:2
'''

If there are many user that will enter data you have to return data out off this(your funtction) to save student marks count.
No if you run mark() several times it will off course reset student count every time.

If this is a school assignment is better that you copy in the correct text for assignment so we dont have to guess.
And come upp with something better than your first code.

If you want to print it out, then it's best to print out how many people got a certain amount of marks before starting the function again. Then you have no way of retrieving the data if you write over it. Also, you need:
if finish == no:

instead of

if:
finish = no

I'm sorry if this doesn't help but I don't get what you mean by making the function 'count' and how the user enters stuff. Could you please explain better, and I could help you further.

Thanks
Mark

No is far from right,this you should know if you try to run it.

Look at this one more time,to you think this will run?

if:
    finish=no
print " student(s) got 0 marks
 student(s) got 1 marks
 student(s) got 2 marks
 student(s) got 3 marks
 student(s) got 4 marks
 student(s) got 5 marks"

And this with singel quote will no work """ my multiline text """ .

Just to show one way to count.

def mark():    
    m1,m2 = 0,0  
    for i in range(5):  #just for test ask 5 times,you can use a while loop and breake out if no
        marks = raw_input("please enter the marks: ")        
        if marks == '1':            
            m1 += 1  #a counting method        
        elif marks == '2':            
            m2 += 1           
    print '%s student got:1' %  (m1)
    print '%s student got:2' %  (m2)
mark() 

'''Out-->
please enter the marks: 1
please enter the marks: 1
please enter the marks: 1
please enter the marks: 2
please enter the marks: 2
3 student got:1
2 student got:2
'''

If there are many user that will enter data you have to return data out off this(your funtction) to save student marks count.
No if you run mark() several times it will off course reset student count every time.

If this is a school assignment is better that you copy in the correct text for assignment so we dont have to guess.
And come upp with something better than your first code.

here is the actual question:

A test at a university is marked out of 5. Write a function testMarks that
allows a lecturer to input the unit marks of all her students, providing a
suitable way for her to say she's finished entering marks. The function
should then output the number of students who have achieved each
possible mark. For example, if the lecturer enters the marks 4, 0, 4, 1,
1, 4, 3 and 5, the function should output:
1 student(s) got 0 marks
2 student(s) got 1 marks
0 student(s) got 2 marks
1 student(s) got 3 marks
3 student(s) got 4 marks
1 student(s) got 5 marks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.