试图在python中计算名称最高的分数 [英] Trying to calculate highest marks with name in python

查看:77
本文介绍了试图在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))])

我想打印分数最高的学生的名字.除了列表之外,我什么都没有学到.没有字典..什么都没有.我想知道我该怎么做? 我也收到此错误!!! builtins.AttributeError:"str"对象没有属性"append".帮助.谢谢你! :D

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

推荐答案

for number in range (1,number+1):

不要将变量名重复用于其他事物,请调用其中一个numbers并调用另一个number:

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):

  • 您在开头将name列为列表:

  • 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)
    

    这是不可能的,因为name在输入之后不再是列表而是字符串.再次为不同的事物使用不同的变量名将有所帮助.调用数组names和单个输入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)
    

  • 在这里:

  • And here:

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

    list是内置类型,不是您的变量之一,因此您要尝试的是对 type 进行索引,而不是特定的列表.如果要在特定列表上调用index,请使用该列表的变量名来执行. grade.index(...)将在grade中找到与通过的成绩相匹配的特定位置,然后您可以使用该位置获取相应的名称,因为您知道该名称在names中处于相同位置:

    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))])
    

  • 这篇关于试图在python中计算名称最高的分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆