这是一个正在进行中的工作代码。你可以说,我是一个完全的初学者,并一直在尝试新事物。我经常得到一个错误,我尝试解决这个问题,但这里没有给我任何错误。只是平均值的答案是错误的数字。没有任何错误,我很难知道代码有什么问题。有什么建议吗?
sum = 0.0
count = 0
ids = [ ]
scores = [ ]
ids = eval(input("Enter an id number:"))
if ids <= 0.0:
print("Can't be 0 or a negative number, please enter a number greater than 0")
exit(1)
scores = eval(input("Enter a score:"))
while ids >= 0.0:
ids = eval(input("Enter another id number (or 0 to quit):"))
if ids <= 0.0:
break
scores = eval(input("Enter another score:"))
sum = sum + scores
count = count + 1
avg = sum / count
for i in range(ids):
print(i)
print("The average of the scores is", avg)
您的代码中存在多个错误。
首先在python中你必须使用右缩进。
接下来你没有任何理由宣布ids = [ ]
和scores = [ ]
你为什么用id?
而不是eval(input())
,使用int(input())
一个计算平均值的简单代码:
scores = list(map(int, input('Enter Space Separated Elements : ').split()))
print('Average is ' + str(sum(scores) / len(scores)))
说明:
希望这可以帮助!