第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

翻翻過去那場雪 2023-06-02 16:05:08
所以..這個項目我需要一些幫助。我開始學(xué)習(xí)一些關(guān)于 python 的知識,我們的討論是關(guān)于文件處理的。我做了正確的執(zhí)行,但這里的問題是我需要找到給定循環(huán)的總和和平均值。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貢獻(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()


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

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}")


查看完整回答
反對 回復(fù) 2023-06-02
  • 2 回答
  • 0 關(guān)注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號