亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何找到文件處理中的平均值之和

如何找到文件處理中的平均值之和

翻翻過去那場雪 2023-06-02 16:05:08
所以..這個項目我需要一些幫助。我開始學習一些關于 python 的知識,我們的討論是關于文件處理的。我做了正確的執行,但這里的問題是我需要找到給定循環的總和和平均值。def num3():    ifile = open(r'sales.txt','w')    no_days = 7    for count in range(1, no_days+1):         print('List of Sales in Php')         sales = float(input('Day' +str(count)+ ':\tPhp'))         ifile.write(str(sales)+'\n')            ifile.close()num3()
查看完整描述

2 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

這應該工作:


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()


查看完整回答
反對 回復 2023-06-02
?
一只甜甜圈

TA貢獻1836條經驗 獲得超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}")


查看完整回答
反對 回復 2023-06-02
  • 2 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號