Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

class student(object):

    def student(self):
        self.name=input("enter name:")
        self.stno=int(input("enter stno:"))
        self.score=int(input("enter score:"))
    def dis(self):
        print("name:",self.name,"stno:",self.stno,"score:",self.score)
    def stno(self):
        return self.stno
    def name(self):
        return self.name
    def score(self):
        return self.score


y=[]
j=0
while(j<3):
    a=student()
    a.student()
    y.append(a)
    j+=1


for st in y:
    st.dis()

for b in y:
    max_v=b.score
    if max_v<b.score:
        max_v=b.score
print(max,b.stno,b.score)

I write above code, but I think there is a problem with finding maximum number amongst numbers as I am trying this code and I cannot find any solution for that. Do you have any opinion to improve this part of code. Many Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
231 views
Welcome To Ask or Share your Answers For Others

1 Answer

Similar to Rawing's answer, but instead of a lambda, you can use operator.attrgetter()

from operator import attgetter

class ...
    # You class code remains unchanged

y=[]
j=0
while(j<3):
    a=student()
    a.student()
    y.append(a)
    j+=1


max_student = max(y, key=attrgetter('score'))
print("Highest score:", max_student.name, max_student.score)

Produces output like this:

enter name:dan
enter stno:3
enter score:3
enter name:emily
enter stno:20
enter score:20
enter name:frank
enter stno:1
enter score:1
Highest score: emily 20

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...