-2
grade=[]
names=[]
highest=0


#taking number of calues

cases=int(input('Enter number of cases: '))
for case in range(1,cases+1):
    print('case',case)

    #taking number of students

    number=int(input('Enter number of students: '))
    for numbers in range (1,number+1):

        #getting name and marks
        name=str(input('Enter name of student: '))
        names.append(name)
        mark=float(input('Enter mark of student:'))
        grade.append(mark)


    print('Case',case,'result') 

    #printing the results!
    average=float(sum(grade)/number)
    print('Average is: %.2f '%(average))
    print('Highest Score is: %.2f'%(max(grade)))
    print('Student with highest score: ',names[grade.index(max(grade))])

output->Enter number of cases: 2
case 1
Enter number of students: 2
Enter name of student: josh
Enter mark of student:98
Enter name of student: sarah
Enter mark of student:87
Case 1 result
Average is: 92.50 
Highest Score is: 98.00
Student with highest score:  josh
case 2
Enter number of students: 3
Enter name of student: shania
Enter mark of student:78
Enter name of student: arleen
Enter mark of student:89
Enter name of student: zoya
Enter mark of student:89
Case 2 result
Average is: 147.00 
Highest Score is: 98.00
Student with highest score:  josh

MY avg. in 3 cases is screwed up also it doesn't show the highest!. I was wondering how can i get the highest if there are 2 same occurrences. Highest will be only the first occurrence. DO you guys get what i mean?

user3295864
  • 49
  • 2
  • 9
  • Can you explain what you mean? What output are you actually getting, and what output do you expect? What is your input? – SethMMorton Feb 11 '14 at 06:16
  • if you see for case 2 the average is 147 and the highest score is 98. which is not possible – user3295864 Feb 11 '14 at 06:16
  • 1
    As a side note, on the line `for numbers in range (1,number+1):`, you don't need to start at `1`. You can just do `for numbers in range (number):`. The value of `numbers` doesn't matter because you never use it. – SethMMorton Feb 11 '14 at 06:16
  • Can you format that block of text so it is readable? I couldn't see that what was going on in there. – SethMMorton Feb 11 '14 at 06:17
  • alright. Thank you. I have edited my output to make it more clear for you! – user3295864 Feb 11 '14 at 06:17

2 Answers2

2

The problem is that in the second iteration through the case loop, you are are also looking at the names and grades from the previous loop because you never emptied the lists. You need to empty the lists at the beginning of the loop. Create the empty lists at the top of the loop:

for case in range(1,cases+1):
    print('case',case)
    grade=[]
    names=[]
    highest=0

You can (and should) do this instead of defining the variables at the top of the file.


To see what was going wrong (before adding my fix), do this:

average=float(sum(grade)/number)
print(grade)
print(names)
print('Average is: %.2f '%(average))
print('Highest Score is: %.2f'%(max(grade)))
print('Student with highest score: ',names[grade.index(max(grade))])

You will see that the lists have the values from the previous iteration through the loop.

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
  • oh never mind i understood what you just explained. IT WOKRED!!! THANK you but could you explain why it worked in 'NOOB' language. Dont really know what iteration means. First language douse – user3295864 Feb 11 '14 at 06:22
  • Take a look at what I suggest to discover what went wrong, and hopefully you will see for yourself what went wrong. It should be pretty obvious. – SethMMorton Feb 11 '14 at 06:25
  • The code inside the loop happens `cases` number of times. Each time it executes the code in the loop, it is an iteration. There are `cases` iterations. – SethMMorton Feb 11 '14 at 06:27
  • @user3295864 Did you try my experiment? Did you discover what the problem was? – SethMMorton Feb 11 '14 at 06:35
  • YES :). You said that the info was already there from the previous loop (i.e. loop 1). So if i put what you said, it will then empty the list at every loop. THANKS A LOT :) you guys are awesome – user3295864 Feb 11 '14 at 06:41
0

Basically, the problem is that the list is filled at the beginning of the loop itself, attempt clearing the list before the loop iteration. Inorder to do so:

for case in range(1,cases+1):
     print(`case`,case)
     grade=[]
     names=[]
     highest=0

The second iteration through the case loop makes a problem here

D3X
  • 547
  • 3
  • 20