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

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

在不使用 .pop 的情況下替換列表中的項(xiàng)目?

在不使用 .pop 的情況下替換列表中的項(xiàng)目?

慕運(yùn)維8079593 2021-07-05 18:07:47
這是問題:一位教授覺得考試成績有點(diǎn)低。教授決定給70分以下的學(xué)生加8%的學(xué)分,給70分以上的學(xué)生加5%的學(xué)分。編寫一個(gè) Python 程序:將分?jǐn)?shù)存儲在列表中,并打印初始分?jǐn)?shù),并顯示一條消息:分?jǐn)?shù) = [73.75, 39.45, 72.60, 45.50, 82.75, 97, 54.50, 48.00, 96.50 ]使用循環(huán),處理每個(gè)分?jǐn)?shù)以計(jì)算新分?jǐn)?shù)并將其存儲在同一個(gè)列表中。您不能更改列表的順序,因?yàn)榉謹(jǐn)?shù)對應(yīng)于班級名冊。添加額外學(xué)分后,將每個(gè)新分?jǐn)?shù)截?cái)酁椴怀^ 100。打印新樂譜列表,并附上一條短消息您不能在您的程序中使用多個(gè)列表。您的程序必須處理任意長度的列表。上面的列表僅用于您的程序測試。我的代碼:#this code shows old scores and prints the new scores of students position = 0 #initialize position for later scores = [73.75, 39.45, 72.60, 45.50, 82.75, 97, 54.50, 48.00, 96.50 ]print ("\nThese are the old scores: ", scores)for score in scores:    if score < 70:        score *= 1.08    elif score >= 70:        score *= 1.05    scores.insert (position,float(format(score,".2f"))) #this adds the new score into position    position += 1    scores.pop (position) #this removes the old score which was pushed to +1 positionfor position, score in enumerate(scores):    if score > 100:        scores[position] = 100print ("These are the new scores:", scores)他希望我不要使用 .pop 或 enumerate 之類的東西,并說有一種更簡單的方法可以做到,但我想不出一個(gè)方法。請幫忙!
查看完整描述

2 回答

?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊

使用range(len)到位歷數(shù)


scores = [73.75, 39.45, 72.60, 45.50, 82.75, 97, 54.50, 48.00, 96.50 ]

print(scores)


for i in range(len(scores)):

    if scores[i] < 70:

        scores[i] = round(scores[i]*1.08, 2)

        if scores[i] > 100:

            scores[i] = 100

    elif scores[i] > 70:

        scores[i] = round(scores[i]*1.05, 2)

        if scores[i] > 100:

            scores[i] = 100


print(scores)

# [77.44, 42.61, 76.23, 49.14, 86.89, 100, 58.86, 51.84, 100]


查看完整回答
反對 回復(fù) 2021-07-13
?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

看看你的第二個(gè)循環(huán):你就是這樣做的。只需直接用新值替換舊值即可。


for i in range(len(scores)):

    if scores[i] < 70:

        scores[i] *= 1.08

    elif scores[i] >= 70:

        scores[i] *= 1.05

沒有了insert和pop。


查看完整回答
反對 回復(fù) 2021-07-13
  • 2 回答
  • 0 關(guān)注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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