简体   繁体   English

在python中使用def函数查找平均值和高于平均值

[英]find average value and above average value using def function in python

here is my code so far. 到目前为止,这是我的代码。 I'm not sure whether I do something wrong on the code because the average seems to be wrong. 我不确定我在代码上是否做错了什么,因为平均值似乎是错误的。 please help me. 请帮我。 Thank you 谢谢

def enter_score ():
    results = []
    scores = int(input("How many results to enter? : "))
    for i in range(scores):
        student_name = input("enter student name: ")
        student_score = int(input("Please enter score for student " + student_name + " : " ))
        results.append(student_score)
        results.append(student_name)
        print(results)
    return results

def calc_average():
    total=0
    total=total+student_score
    average= total/scores
    print("the average is ", average)
    return
def above_average():
  above_average=0
  for i in range (scores):
    if results [i] > average:
        above_average = above_average + 1
        print(" the above average score is ", above_average)
    return above_average

enter_score()
calc_average()
above_average()

You're making a list results that contains scores and names alternating -- very hard to use. 您正在制作包含分数和名称交替的列表results -很难使用。 You return that list from enter_score , then completely ignore -- you throw it away! 您从enter_score返回该列表,然后完全忽略-将其丢弃! So the other two functions are supposed to work on some magic, or thin air...? 因此,其他两个功能应该可以在魔术或稀薄的空气中工作……?

Clearly, the overall flow at the end must instead be: 显然,最后的总流程必须改为:

results = enter_score()
average = calc_average(results)
above_average(results_average)

and calc_average must end with return average . calc_average必须以return average结尾。

results is better arranged by replacing the two results.append calls with a single one: 通过将两个results.append替换为一个results可以更好地安排results.append

results.append((student_score, student_name))

ie, make it a list of tuple, not a weird mix of numbers and names. 即,使其成为元组列表,而不是数字和名称的奇怪组合。

The other two functions clearly must loop on that list (which they now receive as an argument) to do their respective jobs. 其他两个函数显然必须在该列表上循环(它们现在作为参数接收)以完成各自的工作。

So: 所以:

def calc_average(results):
    total = 0
    for student_score, student_name in results:
        total=total+student_score
    average= total/float(len(results))
    print(average)
    return average

and: 和:

def above_average(average, results):
    above_average = 0
    for student_score, student_name in results:
        if student_score > average:
            above_average += 1
    print(" the number of above average scores is ", above_average)
    return above_average

I fixed/ammended your code so that it works: 我修复/修改了您的代码,使其可以正常工作:

def enter_score ():
    results = []
    scores = int(input("How many results to enter? : "))
    for i in range(scores):
        student_name = input("enter student name: ")
        student_score = int(input("Please enter score for student " + student_name + " : " ))        
        results.append((student_name, student_score))
        print(results)
    return results

def calc_average(results):
    total=0
    for student_name, student_score in results:
        total=total+student_score
    average= total/len(results)
    print("the average is ", average)
    return average

def above_average(results, average_score):
    above_average_no=0
    for student_name, student_score in results:
        if student_score > average_score:
            above_average_no = above_average_no + 1
            print(" the above average score is ", above_average_no)
    return above_average_no

results = enter_score()
average_score = calc_average(results)

above_average_no = above_average(results, average_score)

I wont provide detailed explanation on what and why things changes. 我不会提供有关事物发生变化的原因的详细解释。 Leave it to you to figure it out. 留给您自己解决。 Please note that I tried to make minimal changes to your code. 请注意,我试图对您的代码进行最小的更改。 Many things could be improved, such as calculating sum , etc. Hope this helps. 许多事情都可以改进,例如计算sum等等。希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM