在python3中平均不计算权限

问题描述 投票:-3回答:1

这是一个正在进行中的工作代码。你可以说,我是一个完全的初学者,并一直在尝试新事物。我经常得到一个错误,我尝试解决这个问题,但这里没有给我任何错误。只是平均值的答案是错误的数字。没有任何错误,我很难知道代码有什么问题。有什么建议吗?

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 python-3.x
1个回答
0
投票

您的代码中存在多个错误。

首先在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)))

说明:

  1. 了解input here
  2. 了解input().split() here
  3. 了解map() here
  4. map返回一个地图对象时,我们必须将其转换为list
  5. 了解sumlen和其他内置函数here

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.