【问题标题】:How to get average, highest and lowest values of input values如何获得输入值的平均值、最高和最低值
【发布时间】:2021-02-17 20:30:53
【问题描述】:

编写一个 Python 程序,它可以存储学生的姓名和他们的考试成绩。该程序应该能够:

  1. 输入学生姓名和分数后,计算学生的平均分数并打印平均分数。
  2. 总结谁获得最高和最低。

我不知道如何找到平均值、最高和最低,这是我的代码:

students = {}

polling_active = True

while polling_active:
    name = input("enter your name: ")
    score = int(input("enter your score: "))
    
    students[name] = score
    
    repeat = input("would you like to add another student?" "(yes/no)")
    if repeat == 'no':
        polling_active = False
        
print ("-------RESULT-------")
for name, score in students.items():
    print(name, "your score is: ", score )
    
total_sum = float(sum(score)) 
print (total_sum)

【问题讨论】:

  • 您好,简单的语言,您可以在这里发布您的编程问题。问完整的解决方案不是一个好主意。
  • 请不要指望人们会为您完成所有工作。这不是 stackexchange 的用途。向我们展示您的尝试以及您面临的问题。在那里,人们会提供帮助。
  • 哦,对不起,我忘了提到我的工作,我已经编辑了帖子

标签: python variable-assignment


【解决方案1】:

使用以下代码继续您的代码,以获得平均值和最大最小值:

total_sum = sum(list(students.values()))/len(students)
highest=max(students, key=lambda x:students[x])
lowest=min(students, key=lambda x:students[x])

print('Average: ', total_sum)
print('Highest score: ', highest, '   ',students[highest])
print('Lowest score: ', lowest, '   ',students[lowest])

【讨论】:

    【解决方案2】:
    students = {}
    
    def Average(lst): 
        return sum(lst) / len(lst)
    
    while True:
        name = input("enter your name: ")
        score = int(input("enter your score: "))
    
        students[name] = score
    
        repeat = input("would you like to add another student?" "(yes/no)")
        if repeat == 'no':
            break
        
    print ("-------RESULT-------")
    for name, score in students.items():
        print(name, "your score is: ", score )
    
    
    
    
    avg = Average(students.values())
    
    highest = max(students,key=students.get)
    
    lowest = min(students,key=students.get)
    
    print ('avg :',avg)
    print ('highest :',highest)
    print ('lowest :',lowest)
    

    【讨论】:

      【解决方案3】:

      这个问题有很多可能的解决方案。我会详细描述的。

      import sys
      students = {}
      
      polling_active = True
      
      while polling_active:
          name = input("enter your name: ")
          score = int(input("enter your score: "))
          
          students[name] = score
          
          repeat = input("would you like to add another student?" "(yes/no)")
          if repeat == 'no':
              polling_active = False
              
      print ("-------RESULT-------")
      #We will use totalScore for keeping all students score summation.
      #Initaly total score is 0.
      totalScore = 0
      #For getting lowest score student name, we need lowest score first. 
      #Initaly we don't know the lowest score. So we are assuming lowest score is maximum Int value
      lowestScore = sys.maxsize 
      #We also need to store student name. We can use lowestScoreStudentName variable for storing student name with lowest score
      #Initaly we don't know the student name. So we initialize it as an empty student list.
      lowestScoreStudentName = []
      #For getting maximum score student name, we need maximum score first. 
      #Initaly we don't know the maximum score. So we are assuming lowest score is minimum Int value
      maximumScore = -sys.maxsize - 1
      #We also need to store student name. We can use maximumScoreStudentName variable for storing student name with maximum score
      #Initaly we don't know the student name. So we initialize it as an empty student list.
      maximumScoreStudentName = []
      
      for name, score in students.items():
          totalScore = totalScore + score
          print(name, "your score is: ", score )
          if lowestScore > score:
              lowestScore = score
              #Making student list empty, since score is lower than before
              lowestScoreStudentName = []
              lowestScoreStudentName.append(name)
          elif lowestScore == score:
              #keeping all students in the list who gets lowest score
              lowestScoreStudentName.append(name)
              
          if maximumScore < score:
              maximumScore = score
              #Making student list empty, since score is higher than before
              maximumScoreStudentName = []
              maximumScoreStudentName.append(name)
          elif maximumScore == score:
              #keeping all students in the list who gets highest score
              maximumScoreStudentName.append(name)
          
      total_sum = sum(list(students.values()))
      average_sum = totalScore/len(students) 
      
      print("Average score : " + str(average_sum))
      print("Lowest Score holder students name: " + str(lowestScoreStudentName))
      print("Highest score holder students name: " + str(maximumScoreStudentName))
      

      【讨论】:

        【解决方案4】:

        我想这可能会有所帮助

        student = []
        student_1=[]
        score = []
        copy = [] 
        loop = True
        yes = ["YES","yes","y","Y"]
        while loop:
            Name = input("Name : ")
            Score = int(input("Score : "))
            student.append(Name)
            score.append(Score)
            copy.append(Score)
            Enter_more = input("Write YES or Y or y or yes to add more names: ")
            if Enter_more in yes:
                continue
            else:
                break
        score.sort()
        score.reverse()
        sum = 0
        for i in range(len(score)):
            index = score[i]
            student_1.append(student[copy.index(index)])
            sum+=score[i]
            score[i] = student[i]
        print("Average Score is "+str(float(sum/len(score))))
        print("Highest Score is Scored by  "+str(student_1[0]))
        print("Lowest Score is Scored by "+str(student_1[len(student_1)-1]))
        

        【讨论】:

          【解决方案5】:

          您可以直接将您的while循环设置为True,以省去制作另一个变量的麻烦:

          while True:
          

          并打破它

              name=input("Enter your name: ")
              if name=='no':
                  break
          

          找出你能做到的平均水平

          average=(sum(students.values()))/len(students)
          

          最后,对于最高和最低,您可以使用内置的 max 和 min 函数

          highest=max(students.values())
          lowest=min(students.values())
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-10-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-06-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多