【发布时间】:2018-04-08 16:55:18
【问题描述】:
因此,任务是询问用户他们想要在列表中随机生成多少个数字,然后从该列表中找到:总数(总和)、平均值、最小和最大数字。 SOFAR,我在第 14 行收到错误“'int' 类型的对象没有 len()”。使用
import random
def main():
randomList = 0
smallest = 0
largest = 0
average = int(input("How may numbers (between 1-100) would you like to generate?: "))
total = 0
if average >= 101 or average <= 0:
print("Invalid input:How may numbers (between 1-100) would you like to generate?: ")
else:
while randomList != len(int(average)):
randomList.append(random.randint(1,101))
randomList=sorted(randomList)
print(randomList)
total = sum(randomList)
average = float(sum(randomList)) / max(len(randomList))
largest = randomList.pop(average)
smallest = randomList.pop(0)
print('The total of all the numbers are ',str(total))
print('The average of all the numbers are ',str(average))
print('The largest of all the numbers are ',str(largest))
print('The smallest of all the numbers are ',str(smallest))
main()
【问题讨论】:
-
您可能会收到错误消息,因为您正在执行
len(int(average)),正如错误所示。可能值得将您的循环更改为其他内容? -
average是一个数字,不能在上面应用len -
查看有问题的代码行,并用您自己的英文单词向自己解释它应该如何工作。然后逐步分解它,并解释它实际上是如何尝试按书面方式工作的。注意到断开了吗?
标签: python python-3.x list random