简体   繁体   English

计算Python的平均成绩?

[英]Calculating the average grade in Python?

I am trying to get the average of two passing grades. 我试图获得两个及格分数的平均值。 The output should be: 输出应为:

  • 0.0 if neither of the grades is a passing grade both <50 如果两个年级都不是及格分数均小于50,则为0.0
  • The passing grade, if only one of the grades is a passing grade (if one is >50) 及格分数,如果只有其中一个分数为及格分数(如果> 50)
  • The average of the two grades, if both are passing grades (if both are greater than 50) 如果两个年级均为及格成绩(如果两个均大于50),则为两个年级的平均值

Here is my code so far: 到目前为止,这是我的代码:

def passing_grade(grade1,grade2):
    '''(number, number)--> number
    This function definition prints the average of all passing grade(s)
    '''
    # Function 1 - If both numbers are outside the grading range (0-100)
    if 0.0 < grade1 > 100.0 and 0 < grade2 > 100.0:
           print ('Not available grading')

    elif 0.0 >= grade1 <= 50.0 and 0.0 >= grade2 <= 50.0:
            print (0.0)

    #Function 2 - If one of the grades is passing then, print passing grade
    elif 0.0 >= grade1 <= 50.0 and 0.0 >= grade2 >= 50.0:
        print (grade2)
    elif 0.0 >= grade1 >= 50.0 and 0.0 >= grade2 <= 50.0:
        print (grade1)

    #Function 3 - If both grades are passing >50 then print the average
    elif 50.0 > grade1 <= 100.0 and 50.0> grade2 <= 100.0:
        print ((grade1+grade2)/2)

I'm just guessing about your problem here, since you haven't specified, but it looks like you bad logic in the second part of "Function #2": 由于您尚未指定,我只是在这里猜测您的问题,但是在“功能2”的第二部分中看起来您的逻辑不好:

elif 0.0 >= grade1 <= 50.0 and 0.0 >= grade2 >= 50.0:
    print (grade2)
elif 0.0 >= grade1 >= 50.0 and 0.0 >= grade2 <= 50.0:
    print (grade1)

Should be: 应该:

elif grade1 <= 50.0 and grade2 >= 50.0:
    print (grade2)
elif grade1 >= 50.0 and grade2 <= 50.0:
    print (grade1)

If you look at your original conditions, you keep checking 0.0 >= gradeN , which means is only true if the grade is a negative number. 如果您查看原始条件,则继续检查0.0 >= gradeN ,这意味着仅当等级为负数时才为true。 There are similar problems in some of your other sections. 您的其他某些部分也存在类似的问题。

Your comparisons are screwed up. 您的比较搞砸了。 They don't say what you mean, and many evaluate to False always. 他们没有说您的意思,而且许多人总是认为False。 There can be no grade1 such that 0.0 >= grade1 >= 50.0 , as there are no nonpositive numbers greater-equal 50. I suggest you write out your multiple comparisons "the long way" until you're clear about what you mean to say, rather than using this keystroke-saving feature of Python. 不能存在等级1使得0.0 >= grade1 >= 50.0 ,因为不存在大于等于50的非正数。我建议您“漫长的路途”写出多个比较,直到您清楚要说的是什么,而不是使用Python的这种节省按键功能的功能。 a < b < c in Python means a < b and b < c not a < b or b < c which is the form of what you want to say in your first 'if' statement. 在Python中, a < b < c表示a < b and b < c而不是a < b or b < c ,这是您要在第一个“ if”语句中说的形式。

Finally, when writing multiple comparisons in one expression, don't mix directions of the comparisons, it's needlessly confusing (for you, to start with). 最后,在一个表达式中编写多个比较时,不要混淆比较的方向,这无疑会造成混淆(对您而言,从一开始)。

A more concise way of writing (the calculation part of) your function: 一种更简洁的函数编写方法(计算的一部分):

def avg_passing_grade(grade1, grade2):
    passing_grades = [g for g in (grade1, grade2) if 50 <= g <= 100]
    return sum(passing_grades)/max(1, len(passing_grades))

This makes a list passing_grades containing only the grades supplied to the function that are passing. 这将使列表passing_grades仅包含提供给通过的函数的等级。 The function returns their average, taking care not to divide by 0 in case no grades are passing. 该函数返回其平均值,请注意不要在不通过任何等级的情况下将其除以0。

Although the following may be overkill, it's within such easy reach that I have to mention it: the function above generalizes easily to one that takes an arbitrary number of grades: 尽管以下内容可能会过大,但我不得不提一下它:在上面的函数可以轻松地概括为需要任意等级的分数:

def average_passing_grade(* grades):
    '''Return the average of the passing grades among grades.'''
    passing_grades = [g for g in grades if 50 <= g <= 100]
    return sum(passing_grades)/max(1, len(passing_grades))

which you can use like this: 您可以这样使用:

>>> average_passing_grade()
0.0
>>> average_passing_grade(35.3)
0.0
>>> average_passing_grade(75.5)
75.5
>>> average_passing_grade(35.3, 88)
88.0
>>> average_passing_grade(88, 20)
88.0
>>> average_passing_grade(50, 100)
75.0
>>> average_passing_grade(40, 50, 60, 70, 80, 90)
70.0

Besides the bad logic pointed out in the other answers, you could use max and min to do a single logical check for certain cases. 除了其他答案中指出的不良逻辑之外,您还可以使用max和min对某些情况进行一次逻辑检查。

if grade1 >= 50.0 and grade2 >= 50.0: 

can be

if min (grade1, grade2) >= 50.0: # Both are >= 50.0

similarly 同样

if  max(grade1, grade2) < 50.0 # both are less than 50.0

Once those two have been shown false, the the else means that one is on each side of the limit. 一旦这两个显示为假,则else表示在限制的每一侧。

Similarly, to test for the invalid values you can use 同样,要测试无效值,您可以使用

if max(grade1, grade2) > 100.0 or min(grade1, grade2) < 0:

means that at least one grade is invalid 表示至少一个等级无效

if min(grade1, grade2) > 100 or max(grade1, grade2) <0:

means that both grades are invalid in the same way. 表示两个等级都以相同的方式无效。

I am no expert in Python. 我不是Python专家。 However, I'm pretty sure your error is because of your if conditions. 但是,我很确定您的错误是由于您的if条件造成的。

    if 0.0 < grade1 > 100.0 and 0 < grade2 > 100.0:

Should be something like 应该是这样的

    if (grade1 < 0 or grade1 > 100) and (grade2 < 0 or grade2 > 100):

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

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