if语句执行不正确

问题描述 投票:0回答:4
labs = 10.8
superquiz = 15.0
test = 51.0
project = 49.5
exam = 53.5

def course_total(labs, superquiz, test, project, exam):
    """Return the total as an unrounded floating-point value"""
    mark2 = labs * 7/100 + superquiz * 14/100 + test * 10/100 + project * 30/100 + exam * 39/100
    return mark2
print (labs * 7/100 + superquiz * 14/100 + test * 10/100 + project * 30/100 + exam * 39/100)



course_total(labs, superquiz, test, project, exam)
if course_total(labs, superquiz, test, project, exam) >= 50/100:
    print("Course total pass: " + "yes" + " "+ "("+'{0:.2%}'.format(course_total(labs, superquiz, test, project, exam) / 100) + ")")
else:
    print("Course total pass: " + "no" + " " + "("+'{0:.2%}'.format(course_total(labs, superquiz, test, project, exam) / 100) +")")

course_total小于50%,但是为什么没有相应地执行此else子句?应该去else子句,课程总及格应该是"no"

python
4个回答
0
投票

将条件更改为此:

if course_total(labs, superquiz, test, project, exam) >= 50.0:
50/100 = .5 #which is less than

course_total(labs, superquiz, test, project, exam) #which gives 43.6709

0
投票

在您的情况下,course_total的结果为43.67,您正在检查它是否大于50/100(0.5)。这应该是50,因为您的函数返回一个百分比。


0
投票

您的结果值43.666,然后再次对其进行测试0.5,您会看到差异,而且您正在计算4次,只需将其保存在变量中就可以计算一次

total = course_total(labs, superquiz, test, project, exam) / 100
print(total)  # 0.43670999999999993
if total >= 50 / 100:
    print("Course total pass: " + "yes" + " " + "(" + '{0:.2%}'.format(total) + ")")
else:
    print("Course total pass: " + "no" + " " + "(" + '{0:.2%}'.format(total) + ")")

0
投票

您正在IF子句中重新检查50/100。与50.0进行比较

也可以删除if语句上方的冗余方法调用,这不是必需的。

course_total(labs, superquiz, test, project, exam)

删除此行。

© www.soinside.com 2019 - 2024. All rights reserved.