2 回答

TA貢獻(xiàn)1856條經(jīng)驗 獲得超17個贊
這應(yīng)該工作:
def num3():
with open('sales.txt','w') as fp:
no_days = 7
print('List of Sales in Php')
total = 0
for count in range(1, no_days+1):
sales = float(input('Day' +str(count)+ ':'))
total += sales
total = total
average = total/no_days
fp.write(f'Total Value: {total}\nAverage Value: {average}')
num3()

TA貢獻(xiàn)1836條經(jīng)驗 獲得超5個贊
您需要將銷售額計算在一個單獨的變量中。然后除以 no_days:
ifile = open(r'`enter code here`sales.txt', 'w')
no_days = 7
total_sales = 0
for count in range(1, no_days + 1):
print('List of Sales in Php')
sales = float(input('Day' + str(count) + ':\tPhp'))
total_sales += sales
ifile.write(str(sales) + '\n')
ifile.close()
print(f"sum={total_sales}")
print(f"average={total_sales / no_days}")
添加回答
舉報