如何在Python中查找成绩单中的百分比?

如何在Python中查找成绩单中的百分比?,python,Python,请引导我;我想打印三个主题的百分比。我做的节目是: subjects = ["Maths","English","Science"] names = ["Talha","Fazeel","Usayd","Mujtuba","Sufyan","Aukasha","Moiz","Mohid&q

请引导我;我想打印三个主题的百分比。我做的节目是:

subjects = ["Maths","English","Science"]
names = ["Talha","Fazeel","Usayd","Mujtuba","Sufyan","Aukasha","Moiz","Mohid","Wasil"]
scores = [[10,8,7],[8,8,6],[7,5,4],[4,0,2],[3,9,4],[7,8,3],[8,7,5],[9,5,7],[8,7,9]]
a=0
while a<len(names):
    highest=scores[a][0]
    subject=subjects[0]
    i=0
    while i<=2:
        if scores[a][i]>highest:
            highest=scores[a][i]
            subject=subjects[i]
        i=i+1
        

    print(names[a]+"'s Highest in",subject+":", highest)
    a=a+1

正如我们所说,你们需要每个学生的总分,这样你们就可以在内环的每次迭代中求和。一旦获得3个科目的总价值

得到总数后,您可以计算百分比

这是更新后的代码

subjects = ["Maths","English","Science"]
names = ["Talha","Fazeel","Usayd","Mujtuba","Sufyan","Aukasha","Moiz","Mohid","Wasil"]
scores = [[10,8,7],[8,8,6],[7,5,4],[4,0,2],[3,9,4],[7,8,3],[8,7,5],[9,5,7],[8,7,9]]
a=0
while a<len(names):
    highest=scores[a][0]
    subject=subjects[0]
    i=0
    total = 0
    while i<=2:
        total += scores[a][i]
        if scores[a][i]>highest:
            highest=scores[a][i]
            subject=subjects[i]
        i=i+1
        percentage = str("{:.2f}".format(total/30*100)) 
    print(names[a]+"'s Highest in",str(subject)+":", str(highest)+" and percetnage is ", percentage)
    a=a+1

通过使用python提供的预构建函数或方法,有很多方法可以改进代码

subjects = ["Maths", "English", "Science"]
names = ["Talha", "Fazeel", "Usayd", "Mujtuba", "Sufyan", "Aukasha", "Moiz", "Mohid", "Wasil"]
scores = [[10, 8, 7], [8, 8, 6], [7, 5, 4], [4, 0, 2], [3, 9, 4], [7, 8, 3], [8, 7, 5], [9, 5, 7], [8, 7, 9]]
a = 0
while a < len(names):
    h_marks = (max(scores[a]))
    subject = subjects[scores[a].index(h_marks)]
    percentage = round(sum(scores[a])/30*100, 2)
    print(f"{names[a]}'s Highest in {subject}: {h_marks} and percentage is: {percentage}%")
    a = a+1
subjects=[“数学”、“英语”、“科学”]
姓名=[“塔哈”、“法泽尔”、“乌塞伊德”、“穆伊图巴”、“苏菲安”、“奥卡沙”、“莫伊兹”、“莫希德”、“瓦西里”]
分数=[[10,8,7]、[8,8,6]、[7,5,4]、[4,0,2]、[3,9,4]、[7,8,3]、[8,7,5]、[9,5,7]、[8,7,9]]
a=0
而a
在此代码中

  • max(list)将返回列表中的最大值
  • 索引(x)将返回列表“列表”中“x”的索引
  • 舍入(x,y)将浮点数“x”与十进制数字“y”舍入
  • f“x的值:{x}”称为字符串格式,它在创建和初始化字符串时将x的值放入字符串中

  • 这个百分比代表什么?根据你的数据,塔尔哈的数学成绩为10分,那么你是如何得出83.4%的分数的呢?看看这些数组,它们代表3门学科的总分数,百分比是这三门学科的分数。根据
    分数[a]
    中的项目计算一个简单的平均值。这很简单-只需添加项目并除以主题的长度,或者在本例中为3。我只是不明白在代码中放在哪里?下面是代码,请查看一次,让我知道是否有任何问题,而代码仅回答可能会回答问题,通过提供代码的上下文、代码工作的原因以及一些文档参考以供进一步阅读,您可以显著提高答案的质量。这尤其适用于OP是初学者并且试图理解他们的代码为什么不起作用的时候。我实际上是一个初学者,这个答案帮助了我,所以你不应该投入精力it@PranavHosangadi@VishalSheth你能进一步简化它吗?你想用哪种方式优化代码。如果是的话,我可以建议你算出常量变量和可变变量。如果你不明白,请告诉我。感谢Hanks@PranavHosangadi收到您的反馈,我会更新并小心。感谢您的回答,但我是初学者,无法理解您在代码中所做的任何事情。--回答好!如果您为OP的进一步阅读添加了每个点的文档链接,那就更好了。另一个pythonization:使用
    zip
    迭代
    名称
    分数
    ,而不是
    while
    @Sara,请查找一些关于此代码中使用的更改和新函数或方法的说明,以及如何在一般情况下应用它们。我完全同意你的建议by@PranavHosangadi,这段代码肯定可以优化并编写得很好,但假设Sara对python相当陌生,这就是为什么要对代码进行基本修改,以便更好地理解代码。ok,没问题@MdZafarHassan。我肯定会尽快学习新功能:)这对我也很有帮助。谢谢
    Talha's Highest in Maths: 10 and percetnage is  83.33                                                                         
    Fazeel's Highest in Maths: 8 and percetnage is  73.33                                                                         
    Usayd's Highest in Maths: 7 and percetnage is  53.33                                                                          
    Mujtuba's Highest in Maths: 4 and percetnage is  20.00                                                                        
    Sufyan's Highest in English: 9 and percetnage is  53.33                                                                       
    Aukasha's Highest in English: 8 and percetnage is  60.00                                                                      
    Moiz's Highest in Maths: 8 and percetnage is  66.67                                                                           
    Mohid's Highest in Maths: 9 and percetnage is  70.00                                                                          
    Wasil's Highest in Science: 9 and percetnage is  80.00                                                                        
                                                                                                                                  
                                                                                                                                  
    ...Program finished with exit code 0
    
    subjects = ["Maths", "English", "Science"]
    names = ["Talha", "Fazeel", "Usayd", "Mujtuba", "Sufyan", "Aukasha", "Moiz", "Mohid", "Wasil"]
    scores = [[10, 8, 7], [8, 8, 6], [7, 5, 4], [4, 0, 2], [3, 9, 4], [7, 8, 3], [8, 7, 5], [9, 5, 7], [8, 7, 9]]
    a = 0
    while a < len(names):
        h_marks = (max(scores[a]))
        subject = subjects[scores[a].index(h_marks)]
        percentage = round(sum(scores[a])/30*100, 2)
        print(f"{names[a]}'s Highest in {subject}: {h_marks} and percentage is: {percentage}%")
        a = a+1