我应该如何在python中设置一个sentinel循环,只有当新输入的数字大于旧输入的数字时,循环才会继续运行?
这就是我现在所拥有的,但我知道这是不对的。
totalScore = 0
loopCounter = 0
scoreCount = 0
newScore = 0
oldscore = newscore - 1
print("Enter the scores:")
score = (raw_input("Score 1"))
while newScore >= oldScore:
newScore = int(raw_input("Score " ) + (loopCounter + 1))
scoreTotal = scoreTotal+newScore+oldScore
scoreCount = scoreCount + 1
loopCounter = loopCounter + 1
averageScore = scoreTotal / scoreCount
print "The average score is " + str(averageScore)
你的代码有很多问题,甚至不能运行。这是一个工作版本,可以做你想做的事情。
使用while循环管理新值和旧值是编码中常见的习惯用法,值得实践。
编辑:我自己搞错了代码行的顺序。代码现在给出了正确的平均值。
事实上,处理这个问题的方法是使用一个列表,而不是每次都扔掉个人的分数。
你需要不断地向用户询问一个新的输入,而每次输入的数字都会更大。您可以使用一个列表来保存输入的每个分数,然后使用^{} built-in function 为您完成以下工作:
相关问题 更多 >
编程相关推荐