【发布时间】:2020-11-09 01:39:40
【问题描述】:
我正在尝试构建一个程序,用户在该程序中以磅为单位输入地球上的重量(进入 ARRAY)。该程序将重量转换为它在月球上的重量。 然后最后,我希望它获取存储的数组并计算输入重量在月球上的平均重量。
使用下面的代码,它给了我“TypeError: 'float' object is not iterable”。 如何计算平均值?任何帮助表示赞赏! 我的代码在这里link
numlist = list()
lbs = float(input("Enter a weight of person (Enter –1 to exit): "))
while (lbs != -1 ):
numlist.append(lbs)
moon_weight = (lbs / 9.81) * 1.622
limited_float = round(moon_weight)
print ('This person weighs', limited_float, 'pounds on the moon.')
lbs = eval(input("Enter a next weight (Enter –1 to exit): "))
average = sum(moon_weight) / len(numlist)
print ('Average of weights on the moon:', average)
编辑: 因为while True 对我来说还是新的,我不想仅仅使用别人的工作。在其他人的大力帮助下,这就是我能够做到的。
numlist = list()
lbs = float(input("Enter a weight of person (Enter –1 to exit): "))
while (lbs != -1 ):
moon_weight = (lbs / 9.81) * 1.622
limited_float = round(moon_weight)
numlist.append(moon_weight)
print ('This person weighs', limited_float, 'pounds on the moon.')
lbs = eval(input("Enter a next weight (Enter –1 to exit): "))
average = round(sum(numlist) / len(numlist))
print ('Average of weights on the moon:', average)
【问题讨论】: