1

I am trying to write a Python program that computes and prints the following :

  • the average score from a list of scores
  • the highest score from a list of scores
  • the name of the student who got the highest score.

The program starts by asking the user to enter the number of cases. For EACH case, the program should ask the user to enter the number of students. For each student the program asks the user to enter the student's name and marks. For EACH case the program reports the average marks, the highest marks and the name of the student who got the highest marks.

Also If there are more than one person with the highest score in a CASE, the program should report the first occurrence only. The average score and the highest score should have exactly 2 decimal places. The output should be as in the sample program output.

What I have been trying so far is the following:

grade=[]
name_list=[]
cases=int(input('Enter number of cases: '))
for case in range(1,cases+1):
    print('case',case)
    number=int(input('Enter number of students: '))
    for number in range (1,number+1):

        name=str(input('Enter name of student: '))
        name_list.append(name)
        mark=float(input('Enter mark of student:'))
        grade.append(mark)


        highest= max (grade)
        average=(sum(grade)/number)
        high_name=grade.index(max(grade))
        print('average',average)
        print('Highest',highest)
        print (high_name)

This is what i have deciphered so far. my biggest problem now is getting the name of the individual with the high score. Any thoughts and feedback is much appreciated. As with respect to the answer posted below, i am afraid the only thing i do not understand is the dictionary function but otherwise the rest does make sense to me.

  • What do you imagine multiplying a case number times the list of existing cases is going to do? – Wooble Feb 11 '14 at 00:04
  • I was hoping that multiplying the case number * list would give me the case's inputted. For example: suppose the input for case =2 so then there would be 2 lists created for 2 cases What i want later is for the two cases to be further explored in terms that i can add the information like: Name of students in case x , Marks of students in x, Avg of students marks, High score, Printing name associated with high score . Then move on to case 2 I have restarted my code such that now its like : – KingShahmoo Feb 11 '14 at 00:05
  • case= int(input('Enter the amount of cases')) cases=[] for case in range (0,case+1): cases.append(case*cases) print (cases) student=int(input(' Enter the number of students : ')) students=[] – KingShahmoo Feb 11 '14 at 00:08
  • You could use the new statistcs module in python 3.4+ http://docs.python.org/3.4/library/statistics.html – razpeitia Feb 11 '14 at 01:53
  • You already have the index of the highest grade in `high_name` now. The highest grade, and the student of that grade will have the same index in the lists. – M4rtini Feb 11 '14 at 01:56
  • But then how would i have that show? It still shows the index – KingShahmoo Feb 11 '14 at 01:58
  • `name_list[high_name]` use the index to get the name from the list of names. You can check out your [classmate's](http://stackoverflow.com/questions/21691313/trying-to-calculate-highest-marks-with-name-in-python?noredirect=1#comment32794372_21691313) question for more help – M4rtini Feb 11 '14 at 01:59
  • @M4rtini i am not quite sure where i would in put that. I would assume it goes at the end and replace " print(highest_name) – KingShahmoo Feb 11 '14 at 02:02

2 Answers2

1

This resembles an assignment, it is too specific on details.

Anyways, the official docs are a great place to get started learning Python. They are quite legible and there's a whole bunch of helpful information, e.g.

range(start, end): If the start argument is omitted, it defaults to0

The section about lists should give you a head start.

MBlanc
  • 1,773
  • 15
  • 31
1
numcases = int(input("How many cases are there? "))

cases = list()

for _ in range(numcases):
    # the _ is used to signify we don't care about the number we're on
    # and range(3) == [0,1,2] so we'll get the same number of items we put in

    case = dict() # instantiate a dict

    for _ in range(int(input("How many students in this case? "))):
        # same as we did before, but skipping one step
        name = input("Student name: ")
        score = input("Student score: ")
        case[name] = score # tie the score to the name

    # at this point in execution, all data for this case should be
    # saved as keys in the dictionary `case`, so...
    cases.append(case) # we tack that into our list of cases!

# once we get here, we've done that for EVERY case, so now `cases` is
# a list of every case we have.

for case in cases:
    max_score = 0
    max_score_student = None # we WILL need this later
    total_score = 0 # we don't actually need this, but it's easier to explain
    num_entries = 0 # we don't actually need this, but it's easier to explain
    for student in case:
        score = case[student]
        if score > max_score:
            max_score = score
            max_score_student = student

        total_score += score
        num_entries += 1
        # again, we don't need these, but it helps to demonstrate!!
    # when we leave this for loop, we'll know the max score and its student
    # we'll also have the total saved in `total_score` and the length in `num_entries`
    # so now we need to do.....
    average = total_score/max_entries

    # then to print we use string formatting

    print("The highest score was {max_score} recorded by {max_score_student}".format(
        max_score=max_score, max_score_student=max_score_student))
    print("The average score is: {average}".format(average=average))
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • +1 for good explanatory comments. I think you made a small typo in the average. should be divided by `num_entries` right? And maybe explain what a dict is, or a link to the docs. Someone starting to learn python may not have seen those yet. – M4rtini Feb 11 '14 at 01:02
  • Amazing code.however i am still a beginner so i am somewhat lost in the sense of dict function is. I have updated my code but i do not know how to post on stackoverflow with the correct formatting – KingShahmoo Feb 11 '14 at 01:28
  • grade=[] name_list=[] cases=int(input('Enter number of cases: ')) for case in range(1,cases+1): print('case',case) number=int(input('Enter number of students: ')) for number in range (1,number+1): name=str(input('Enter name of student: ')) name_list.append(name) mark=float(input('Enter mark of student:')) grade.append(mark) highest= max (grade) average=(sum(grade)/number) high_name=grade.index(max(grade)) print('average',average) print('Highest',highest) print (high_name) I am having ... – KingShahmoo Feb 11 '14 at 01:35
  • problems in getting the name and high score to be associated – KingShahmoo Feb 11 '14 at 01:35
  • @KingShahmoo You can [edit] your question and add that inn there. – M4rtini Feb 11 '14 at 01:38