If statement 无人平均数
当我输入999时,我无法执行中断,它只是不断地要求更多分数和帮助If statement 无人平均数,if-statement,while-loop,printing,If Statement,While Loop,Printing,当我输入999时,我无法执行中断,它只是不断地要求更多分数和帮助 score = 0 总分=0 while True: scores = float(input("Enter test score: ") if score in range (0,100): total_score = score + total_score score = score + 1 elif score = 999:
score = 0
总分=0
while True:
scores = float(input("Enter test score: ")
if score in range (0,100):
total_score = score + total_score
score = score + 1
elif score = 999:
break
else:
print("Test score must be from 0 through 100")
average_score = total_score / score
print("Total score", total_score )
print("Average Score", average_score)
正如@Peter所提到的,使用
score
和scores
是令人困惑的。最好使用像cnt
这样的变量来保持分数计数。请注意,范围(0100)
仅为99。使用范围(0101)
包括100。您还有一些语法错误导致问题
请尝试以下代码:
total_score = 0 # sum of scores
cnt = 0 # score count
while True:
score = int(input("Enter test score (0-100): "))
if score in range (0,101): # 0-100
total_score += score
cnt += 1
elif score == 999:
break
else:
print("Test score must be from 0 through 100")
average_score = total_score / cnt
print("Score count", cnt )
print("Total score", total_score )
print("Average Score", average_score)
输出
Enter test score (0-100): 20
Enter test score (0-100): 30
Enter test score (0-100): 40
Enter test score (0-100): 50
Enter test score (0-100): 60
Enter test score (0-100): 999
Score count 5
Total score 200
Average Score 40.0
那是蟒蛇吗?你应该在你的问题上加上语言标签。我对Python了解不多,但它真的允许这样的不平衡参数吗?也就是说,
float(输入(“输入测试分数”)
。更重要的是,您将用户输入存储在scores
中,然后使用一个名为score
的完全不同的变量与999
进行比较。这可能不起作用,我并不感到惊讶。