0

Not sure what I've done wrong here tried lots of alternative combinations and not sure why this logic doesn't work! Im pretty sure it has something to do with the parameter not reading as the results inputted aren't going through the loop. Any help would be appreciated! The idea of the function is to add up marks scored by diferent people and in the end outputting as shown below.

def Marks(*score):
    marks0 = 0 
    marks1 = 0
    marks2 = 0
    marks3 = 0
    marks4 = 0 
    marks5 = 0
    
    if score == 0:
        marks0 = marks0+1
    elif score == 1:
        marks1 = marks1+1
    elif score == 2:
        marks2 = marks2+1
    elif score == 3:
        marks3 = marks3+1
    elif score == 4:
        marks4 = marks4+1
    elif score == 5:
        marks5 = marks5+1

    print "0 marks scored by", marks0 , "people."
    print "1 mark scored by", marks1 , "people."
    print "2 marks scored by", marks2 , "people."
    print "3 marks scored by", marks3 , "people."
    print "4 marks scored by", marks4 , "people."
    print "5 marks scored by", marks5 , "people."
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by charlottetemp
1

See if adding this print statement clears things up. You have to compare the same type of variables, i.e. an integer to an integer, etc.

def Marks(*score):
    print score, type(score)
    marks0 = 0
    marks1 = 0
    marks2 = 0
    marks3 = 0
    marks4 = 0
    marks5 = 0

    if score == 0:
        marks0 = marks0+1
    elif score == 1:
        marks1 = marks1+1
    elif score == 2:
        marks2 = marks2+1
    elif score == 3:
        marks3 = marks3+1
    elif score == 4:
        marks4 = marks4+1
    elif score == 5:
        marks5 = marks5+1

    print "0 marks scored by", marks0 , "people."
    print "1 mark scored by", marks1 , "people."
    print "2 marks scored by", marks2 , "people."
    print "3 marks scored by", marks3 , "people."
    print "4 marks scored by", marks4 , "people."
    print "5 marks scored by", marks5 , "people."

Marks(1)

Also you can use a list instead of 6 variables.

def Marks(*score_input):
    marks_list = [0, 0, 0, 0, 0, 0]
    for score in score_input:
        if score < 6:
            marks_list[score] += 1
        else:
            print "Bogus score entered ----->", score

    for score in range(6):
        print "%s marks scored by %d people." % \
                 (score, marks_list[score])

Marks(1, 3, 3, 7, 5)

Edited by woooee: n/a

Votes + Comments
very helpful
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.