How can I find max number among numbers in this code?

2024/11/15 6:29:33
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.stnodef name(self):return self.namedef score(self):return self.scorey=[]
j=0
while(j<3):a=student()a.student()y.append(a)j+=1for st in y:st.dis()for b in y:max_v=b.scoreif 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

Answer

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

from operator import attgetterclass ...# You class code remains unchangedy=[]
j=0
while(j<3):a=student()a.student()y.append(a)j+=1max_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
https://en.xdnf.cn/q/119684.html

Related Q&A

Assert data type of the values of a dict when they are in a list

How can I assert the values of my dict when they are in a list My_dict = {chr7: [127479365, 127480532], chr8: [127474697, 127475864], chr9: [127480532, 127481699]}The code to assert this assert all(isi…

Loading tiff images in fiftyone using ipynp

I am trying to load tiff images using fiftyone and python in ipynb notebook, but it just doesnt work. Anyone knows how to do it?

Regular expression to match the word but not the word inside other strings

I have a rich text like Sample text for testing:<a href="http://www.baidu.com" title="leoshi">leoshi</a>leoshi for details balala... Welcome to RegExr v2.1 by gskinner.c…

Make one image out of avatar and frame in Python with Pillow

If I haveandneed to getdef create_avatar(username):avatar, frame, avatar_id = get_avatar(username)if avatar is not None and frame is not None:try:image = Image.new("RGBA", size)image.putalpha…

Could not broadcast input array from shape (1285) into shape (1285, 5334)

Im trying to follow some example code provided in the documentation for np.linalg.svd in order to compare term and document similarities following an SVD on a TDM matrix. Heres what Ive got:results_t =…

Python URL Stepping Returns Only First Page Results

Any help with the below code would be appreciated. I have checked the results of h and g using print to verify that they are incrementing the url properly, but the program seems to be only repeating th…

Text processing to find co-occurences of strings

I need to process a series of space separated strings i.e. text sentences. ‘Co-occurrence’ is when two tags (or words) appear on the same sentence. I need to list all the co-occurring words when they…

Flask doesnt render any image [duplicate]

This question already has answers here:How to serve static files in Flask(24 answers)Link to Flask static files with url_for(2 answers)Closed 6 years ago.I have a flask application where I need to rend…

Bug in python thread

I have some raspberry pi running some python code. Once and a while my devices will fail to check in. The rest of the python code continues to run perfectly but the code here quits. I am not sure wh…

how does a function changes the value of a variable outside its scope? Python

i was coding this code and noticed something weird, after my function has been called on the variable, the value of the variable gets changed although its outside of the functions scope, how exactly is…