私の現在のコード:
mylist = [0] * 7
for x in range(7):
sales=-1
while (sales<0):
sales = float(input("Sales for day {0}:".format(x)))
mylist[x] = sales
if sales < 0:
print ("Sorry,invalid. Try again.")
print (mylist)
best = max(mylist)
worst = min(mylist)
average = sum(mylist)/len(mylist)
total = sum(mylist)
print ("Your best day had", best, "in sales.")
print ("Your worst day had", worst, "in sales.")
print ("Your average daily sales were:", format(average,'.2f'))
print ("Your total sales were:", format(total, '.2f'))
私がそれを実行すると、これが得られます:最初のものは機能します、私の質問は、私がそれを実行するのは約2回目です
Sales for day 0: 5
Sales for day 1:4
Sales for day 2:6
Sales for day 3:7
Sales for day 4:8
Sales for day 5:2
Sales for day 6:3
[5.0, 4.0, 6.0, 7.0, 8.0, 2.0, 3.0]
Your best day had 8.0 in sales.
Your worst day had 2.0 in sales.
Your average daily sales were: 5.00
Your total sales were: 35.00
Sales for day 0: 5
Sales for day 1:4
Sales for day 2:3
Sales for day 3:-4
Sorry,invalid. Try again.
Sales for day 3:four
Traceback (most recent call last):
line 44, in <module>
sales = float(input("Sales for day {0}:".format(x)))
ValueError: could not convert string to float: 'four'
これを再コーディングして、ユーザーが数字の代わりに「4」のようなものを言うことにした場合、Python が別の整数を入力するように再プロンプトするようにするにはどうすればよいでしょうか。