我正在尝试利用 TeamTreehouse 学习订阅和这本《从编程逻辑和设计开始》一书来尝试学习编程和 Python。
目标:合并第 12 行中通过引用传递接收到的输入,然后将传入 testScoreAverage 的每个后续输入添加到先前的值中。相反,它只是保留一个值而不是运行总计。
我添加了注释,逐行表明我的错误/对代码的理解
我真的认为我以某种方式通过将变量设置为数据类型 int 来将它们设置为 0,但我不明白这是怎么回事,因为我过去已经能够做到这一点?
#///////////////Defining Variables/////////////
testScore1=0
testScore2=0
testScore3=0
testScore4=0
testScore5=0
tests=5
testScoreAverage=0
#///////////////Defining Variables/////////////
#///////////////calcAverage Function/////////////
#L2g2h: defines function & receives current testScore int value
def calcAverage(testScore):
#L2g2h: initiates variable testScoreAverage as an int value type (but doesn't set it to any value)
testScoreAverage=int()
#L2g2h: again initiating a variable, variable testScores, to int value type (but not setting it to any numeric value)
testScores=int()
#L2g2h: for loop starting
for i in range(1, tests + 1):
#L2g2h: testScores is set equal to the current testScores value PLUS the current value of testScore to become a running total
testScores=testScores+testScore
#L2g2h: testScoreAverage is dividing the current testScores value by the current counter variable i's value to come up with an average
testScoreAverage=testScores/i
#////NEED TO SET NUMBER TO HOLD ONLY 2 DECIMAL PLACES
#L2g2h: print out the current testScoreAverage
print("Your test score average so far is ", float(testScoreAverage))
#///////////////calcAverage Function/////////////
#///////////////determineGrade Function/////////////
def determineGrade(testScore):
# for i in range(1, tests + 1):
if testScore>=90 and testScore <= 100:
print("Your test score grade is an A.")
elif testScore>=80 and testScore <= 89:
print("Your test score grade is a B.")
elif testScore>=70 and testScore <= 79:
print("Your test score grade is a C.")
elif testScore>=60 and testScore <= 69:
print("Your test score grade is a D.")
elif testScore<=60:
print("Your test score grade is a F.")
else:
print("That is invalid.")
#///////////////determineGrade Function/////////////
for i in range(1, tests + 1):
testScore=int(input("Enter test score #" + str(i)))
calcAverage(testScore)
determineGrade(testScore)
最佳答案
您在最后一个 for
循环中的每次迭代中调用函数 calcAverage
一次。只有在用户输入所有分数值后才应计算平均值。
首先将您的值附加到列表中,然后在用户输入所有值后计算平均值:
#///////////////Defining Variables/////////////
testScore1=0
testScore2=0
testScore3=0
testScore4=0
testScore5=0
tests=5
testScoreAverage=0
#///////////////Defining Variables/////////////
#///////////////calcAverage Function/////////////
def calcAverage(testScore):
testScoreAverage=int()
testScoreAverage= sum(testScore)/len(testScore)
#////NEED TO SET NUMBER TO HOLD ONLY 2 DECIMAL PLACES
print("Your test score average so far is ", float(testScoreAverage))
return testScoreAverage
#///////////////calcAverage Function/////////////
#///////////////determineGrade Function/////////////
def determineGrade(testScore):
# for i in range(1, tests + 1):
if testScore>=90 and testScore <= 100:
print("Your test score grade is an A.")
elif testScore>=80 and testScore <= 89:
print("Your test score grade is a B.")
elif testScore>=70 and testScore <= 79:
print("Your test score grade is a C.")
elif testScore>=60 and testScore <= 69:
print("Your test score grade is a D.")
elif testScore<=60:
print("Your test score grade is a F.")
else:
print("That is invalid.")
#///////////////determineGrade Function/////////////
scores =[]
for i in range(1, tests + 1):
scores.append(int(input("Enter test score #" + str(i)+" ")))
average = calcAverage(scores)
determineGrade(average)
请注意,要计算列表中的平均值,您可以使用 sum
函数对该列表中的所有值求和,然后使用 len(list)< 除以列表长度
如果您想确定每个分数的等级,您可以迭代包含分数的列表并确定每个分数的等级,如下所示:
for score in scores: #scores is the list
determineGrade(score)
关于python - 平均功能不是平均而是重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30016493/