问题
我正在做一个可以告诉你成绩的程序。我已经得到了所有的用户输入,现在我需要做一些计算。当调用“计算加权平均数”时,我的问题就出现了。它将参数作为整数(至少我认为是这样的:我要求它print(x, y, z)
并打印两次,一次作为x,y,z的正确输入,一次作为x的正确输入,但它不打印y和z作为None),但它拒绝执行我要求它做的数学运算。函数不应该接受参数并执行这个简单的操作:( x + y ) * z
?
所以出于某种原因,y和z变成了None,这就是我需要解决的问题!在
代码
def main():
tests = get_initial_input("tests")
assignments = get_initial_input("assignments")
exercises = get_initial_input("exercises")
labs = get_initial_input("labs")
finals = get_initial_input("finals")
testsp = get_percents(tests , "tests")
assignmentsp = get_percents(assignments, "assignments")
exercisesp = get_percents(exercises, "exercises")
labsp = get_percents(labs, "labs")
finalsp = get_percents(finals, "finals")
testst = get_totals(tests, "tests")
assignmentst = get_totals(assignments, "assignments")
exercisest = get_totals(exercises, "exercises")
labst = get_totals(labs, "labs")
finalst = get_totals(finals, "finals")
testss = get_scores(tests, "tests")
assignmentss = get_scores(assignments, "assignments")
exercisess = get_scores(exercises, "exercises")
labss = get_scores(labs, "labs")
finalss = get_scores(finals, "finals")
testsz = calculate_weighted_average(testss, testst, testsp)
assignmentsz = calculate_weighted_average(assignmentss, assignmentst, assignmentsp)
exercisesz = calculate_weighted_average(exercisess, exercisest, exercisesp)
labsz = calculate_weighted_average(labss, labst, labsp)
finalsz = calculate_weighted_average(finalss, finalst, finalsp)
def get_initial_input(x):
val = int(input("How many "+ x + " were there? "))
return val
def get_percents(x, string):
if x > 0:
xp = int(input("What percentage are "+ string + " weighted? "))
return xp
def get_totals(x, string):
if x > 0:
xt = int(input("How many total points available in the "+ string +" category? "))
return xt
def get_scores(x, string):
total = 0
for y in range(x):
xs = int(input("Scores for "+ string +" ?: "))
total = total + xs
return total
def calculate_weighted_average(x, y, z):
print(x, y, z)
this = ( x / y ) * z
return this
main()
回溯
^{pr2}$提前谢谢您
功能
当
x
小于或等于零时返回None
。此值随后传播到calculate_weighted_average
中的数值表达式并将其断开。 修复此函数和其他函数以始终返回数字或引发异常。在您需要调整您的
get_totals
和get_percents
以返回int
,即使它们的条件失败。同样,如果它们的任何一个条件都不满足-None
将被返回。在例如:
同时:
^{pr2}$相关问题 更多 >
编程相关推荐