python3.6:从用户inpu计算的平均值中检测最高/最低值

2024-09-28 01:24:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须创建一个程序,收集成绩,取平均值,然后打印最高和最低的平均值以及用户名。我正在使用前一周作业中的代码,该代码接受用户输入并计算平均值,但我正在努力找出如何从计算出的平均值中检测高/低值。我也不确定在检测到这些高/低值后,从何处开始重新打印相关的名称信息。任何帮助都将不胜感激!你知道吗

def main():

    numuser=eval(input("How many users are there?: "))
    numgrade=eval(input("How many grades will there be for each user?: "))
    usercount=0
    gradecount=0

    grade(numuser,numgrade,usercount,gradecount)

def grade(nu,ng,uc,gc):
    name=[]
    while uc <= nu:
        first=input("Please enter the student's first name: ")
        last=input("Please enter the student's last name: ")
        name.append(last)
        ID=input("Please enter the student's ID: ")
        gradetot=0

        total=[]
        grades=[]
        while gc < ng:
            gradeval=eval(input("Please enter the grade: "))
            total.append(gradeval)
            gradetot=gradetot+gradeval
            gc=gc+1

            avg=gradetot/gc
            grades.append(avg)

            low=min(grades)
            high=max(grades)

        nu=nu-1
        uc=uc+1
        gc=0

        print("The average grade for", first, last, ID, "is :", avg)

    if uc>nu:
        print("The lowest average is ", low, "and the highest average is", high)

main()

Tags: thenameinputevalgcnu平均值first
1条回答
网友
1楼 · 发布于 2024-09-28 01:24:28

你必须把学生姓名和平均成绩的数据储存在字典里。然后可以使用字典方法进行检索

  1. 您使用的是in>;=in主循环,它迭代的次数更多,这意味着要求用户输入更多的记录
  2. 如果你想打印用户名和他们的成绩信息,你必须把他们存储在字典里

    def main():

    numuser=eval(input("How many users are there?: "))
    numgrade=eval(input("How many grades will there be for each user?: "))
    usercount=0
    gradecount=0
    
    grade(numuser,numgrade,usercount,gradecount)
    

    def grade(nu,ng,uc,gc): dct={} while uc < nu: first=input("Please enter the student's first name: ") last=input("Please enter the student's last name: ") ID=input("Please enter the student's ID: ") gradetot=0

        total=[]
        while gc < ng:
            gradeval=eval(input("Please enter the grade: "))
            total.append(gradeval)
    
            average=[]
            gradetot=gradetot+gradeval
            average.append(gradetot)
            avg=gradetot/(gc+1)
            gc=gc+1
            dct[first+last]=avg  
            #Getting errors here with trying to find a mechanism to detect values from a calculated average
            #nu=nu-1
        uc=uc+1
        gc=0
    
    
    print dct    
    maxkey, maxvalue = max(dct.items(), key=lambda x:x[1])
    
    print("The highest grade obtained by", maxkey, "is :",maxvalue )
    

    """ nu=nu-1 uc=uc+1 gc=0

    if uc>nu:
        print("The lowest average is ", low, " and the highest is ", high)
        #Need to print the names that go along with the grades
    

    “”“

相关问题 更多 >

    热门问题