这是我当前的代码:
while True:
try:
mylist = [0] * 7
for x in range(7):
sales = float(input("Sales for day:"))
mylist[x] = sales
if sales < 0:
print ("Sorry,invalid. Try again.")
except:
print ("Sorry, invalid. Try again.")
else:
break
print (mylist)
best = max(sales)
worst = min(sales)
print ("Your best day had", best, "in sales.")
print ("Your worst day had", worst, "in sales.")
当我运行它时,我得到了这个:
Sales for day:-5
Sorry,invalid. Try again.
Sales for day:-6
Sorry,invalid. Try again.
Sales for day:-7
Sorry,invalid. Try again.
Sales for day:-8
Sorry,invalid. Try again.
Sales for day:-9
Sorry,invalid. Try again.
Sales for day:-2
Sorry,invalid. Try again.
Sales for day:-5
Sorry,invalid. Try again.
[-5.0, -6.0, -7.0, -8.0, -9.0, -2.0, -5.0]
Traceback (most recent call last):
File "C:/Users/Si Hong/Desktop/HuangSiHong_assign9_part.py", line 45, in <module>
best = max(sales)
TypeError: 'float' object is not iterable
我不太确定如何对其进行编码,以便列表不接受负值,因为我只想要 0 或更大的值。
我不确定如何解决 TypeError 问题,以便在我的代码中打印最小值和最大值
我的最后一个问题是,如果我想找到用户输入的七个输入的平均值,我应该如何将这些值从列表中提取出来
非常感谢