im currently making a program that takes in the names and scores of the student but i dont know how to get the top 3 scores when the scores starts to get the same
ex student 1 is 100 student 2 is 99 student 3 and 4 is 98 i dont know how to print both student 3 and 4 at the same time.

def getStudent():
    grades = 50
    sgrade = []
    names = []
    h1 =[]
    h2 =[]
    h3 =[]
    fob = open('Student.txt')
    fob_read = fob.readlines()
    for line in fob_read:
        (name,grade) = line.split()
        names.append(name)
        sgrade.append(grade)
        fob.close()
    print(sgrade)
    print(name)

    if grade > grades:
        grades = grade
        h1.append(name+grade)

while True:
    fob = open("Student.txt",'r')
    raw = fob.readlines()
    name = raw_input("Student Name: ")
    grade = input("Grade:")
    if name == "1":   
        if grade == 1:
            break
    if grade >= 50:     
        if grade <= 100: 
            opw = open("Student.txt",'w')
            opw.writelines(raw)
            opw.write(name +" ")
            opw.write(str(grade))
            opw.write('\n')
            opw.close()
        else:
            print("Grade must be 50 - 100 ")
    else:
        print("Grade must be 50 - 100 ")
getStudent()

Recommended Answers

All 8 Replies

Do you mean like this?

a = [5,4,4]
i = 0
while(i<len(a)):
        if (i + 1) <= len(a):
                if a[i] == (a[i+1]):
                        print "Equal grades %s and %s" % (a[i], (a[i+1]))
                i = i + 2
                else:
                        print "Grade %s" % (a[i])
            i = i + 1
        else:
        print "Last item in the list %s" % (a[i])
        i = i + 1

Output:
Grade 5
Equal grades 4 and 4

um no more like something like this
enter student name: A
enter student grade: 100
enter student name: B
enter student grade: 99
enter student name: C
enter student grade: 97
enter student name: D
enter student grade: 99
enter student name: E
enter student grade: 97
enter student name: F
enter student grade: 96

top 3 grades:
A : 100
B : 99
D : 99
C : 97
E : 97

Well, it is kind of the same concept but what you could do is actually save your data in a dictionary {StudentName : Grade} then used sorted to sort it by values in a reversed order so the first element will be the highest.
Then when outputting them make a function get the best 3 grades, where you loop through the values of the dictionary, and save {student : grade} in a new dictionary that you will return before exiting the function. Your looping condition will be that there are 3 different values in the new dictionary that will be returned

can you please give and example of that loop im really bad at that

Well, I'll give you a quick example using list of numbers and a structure called "set", that does not contain the same object multiple times so if it has 3 elements inside, that means 3 different grades have been processed

grades = [5,4,4,5,5,5,3,1,2,3,5,7,1]
aSet = set()
SortedGrades = sorted(grades, reverse = True)
for grade in SortedGrades:
    if len(aSet) >= 3 and grade not in aSet:
        break
    aSet.add(grade)
    print "Student's grade is %s" %(grade)

Output is:

Student's grade is 7
Student's grade is 5
Student's grade is 5
Student's grade is 5
Student's grade is 5
Student's grade is 5
Student's grade is 4
Student's grade is 4

this is my new code please help me sort the values on student by highest to lowest as well as print the top 3 recurrint items

student = {}
top3 = {}
class getstudent():
    def get(self):
        fob = open("Students.txt",'r')
        raw = fob.readlines()
        name = raw_input("Student Name: ")
        grade = input("Grade:")
        if (name == '-1' or grade == -1):
            print 'lol'
            self.sort();
        elif(grade >= 50 and grade <= 100):     
            opw = open("Students.txt",'w')
            opw.writelines(raw)
            opw.write(name +" ")
            opw.write(str(grade))
            opw.write('\n')
            opw.close()
            self.get();
        else:
            print("Grade must be 50 - 100 ")
            self.get();
    def sort(self):
        read = open('students.txt','a+')
        readlines = read.readlines()
        if readlines == []:
            print 'No Student Record'
        else:
            for line in readlines:
                (name1,grade1) = line.split()
                student.update({name1:int(grade1)})

        read.close()
        print student

g = getstudent();
g.get();

here's my new code when ever i sort things out the 100 always go last why is that please help me sort and them and preview the top 3 recurring students.

grades = []
names = [] 
student = {}
top3 = {}
class getstudent():
    def get(self):
        fob = open("Students.txt",'r')
        raw = fob.readlines()
        name = raw_input("Student Name: ")
        grade = raw_input("Grade:")
        if (name == '-1' or grade == '-1'):
            self.sort();
        elif(float(grade) >= 50 and float(grade) <= 100):     
            opw = open("Students.txt",'w')
            opw.writelines(raw)
            grade2 = round(float(grade),2)
            opw.write(name +" ")
            opw.write(str(grade2))
            opw.write("\n")
            opw.close()
            self.get();
        else:
            print("Grade must be 50 - 100 ")
            self.get();

    def sort(self):
        read = open('students.txt','a+')
        readlines = read.readlines()
        if readlines == []:
            print 'No Student Record'
        else:
            for line in readlines:
                (name1,grade1) = line.split()
                grades.append(grade1)
                names.append(name1)
                student.update({name1:float(grade1)})
                top3.update({float(grade1):float(grade1)})

        read.close()
        sortgrade = sorted(grades,reverse=True)
        sortname =  sorted(student, key=student.__getitem__, reverse=True)
        print '\nTop 3 Students: '
        <-- missing sort -- >

g = getstudent();       
g.get();
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.