Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
846 views
in Technique[技术] by (71.8m points)

average - Trying to calculate highest marks with name in python

grade=[]
names=[]
highest=0



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 numbers in range (1,number+1):

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


    print('Case',case,'result') 
    print('name',list[list.index(max(grade))])
    average=(sum(grade)/number)
    print('average',average)
    print('highest',max(grade))
    print('name',names[grade.index(max(grade))])

I want to print name of the student with the highest mark. I have not learned anything other than list, while and for. NO dictionary ..nothing. I was wondering how can i do this? ALSO i am getting this error!!! builtins.AttributeError: 'str' object has no attribute 'append'. HELP. thank you! :D

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)
  1. for number in range (1,number+1):
    

    Don't reuse variable names for different things, call one of them numbers and the other number:

    numbers=int(input('Enter number of students: '))
    for number in range (1,numbers+1):
    
  2. You made name a list in the beginning:

    name=[]
    

    but here you assign a single input to it:

    name=str(input('Enter name of student: '))
    

    The you append the new name to itself:

    name.append(name)
    

    which is not possible, because name is after the input no longer a list but a string. Again using different variable names for different things would help. Call the array names and the single input name:

    names = []
    #...
    name=str(input('Enter name of student: '))
    names.append(name)
    
  3. And here:

     print('name',list[list.index(max(grade))])
    

    list is a build-in type, not one of your variables, so what you are trying to do is index a type, not a specific list. If you want to call index on a specific list you do so by using the variable name of that list. grade.index(...) will find the specific position matching the passed grade in grade and then you can use this position to get the corresponding name, because you know, that the name is at the same position in names:

    print('name',names[grade.index(max(grade))])
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...