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

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

如何更改此代碼以不打印未使用的硬幣=0?

如何更改此代碼以不打印未使用的硬幣=0?

一只名叫tom的貓 2023-10-11 20:03:39
我正在嘗試構(gòu)建一個(gè)功能,當(dāng)人們輸入金額后,它會(huì)顯示他們需要的最小硬幣或紙幣數(shù)量。但這有什么方法可以讓我改變它,這樣它就不會(huì)打印未使用的硬幣的名稱和數(shù)量嗎?(作為初學(xué)者)感謝您的幫助?。梢杂胒or循環(huán)來處理嗎?)
查看完整描述

3 回答

?
慕的地10843

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

循環(huán)可用于遍歷筆記列表,并且在循環(huán)內(nèi),如果發(fā)現(xiàn)有任何筆記被計(jì)數(shù),則可以打印該筆記。


notes=[1000,500,100,50,20,10,5,2,1]


amount=int(input('Enter an amount: '))


print('Total number of notes/coins=')


for notesAmount in notes:

    if amount>=notesAmount:

       notesCount=amount//notesAmount

       amount%=notesAmount

       if notesCount>0:

          print(notesAmount, ":", notesCount)


查看完整回答
反對(duì) 回復(fù) 2023-10-11
?
慕少森

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

不要為每個(gè)面額保留一個(gè)變量,而是保留一個(gè)字典并根據(jù)所使用的面額更新 key: val 。查看代碼


amount=int(input('Enter an amount: '))


denominations = dict()


print('Total number of notes/coins=')


if amount>=1000:

    denominations['1000'] = amount//1000

    amount%=1000    

if amount>=500:

    denominations['500'] = amount//500

    amount= amount%500

if amount>=100:

    denominations['100'] = amount//100

    amount= amount%100

if amount>=50:

    denominations['50'] = amount//50

    amount= amount%50    

if amount>=20:

    denominations['20'] = amount//20

    amount= amount%20    

if amount>=10:

    denominations['10'] = amount//10

    amount= amount%10

if amount>=5:

    denominations['5'] = amount//5

    amount= amount%5   

if amount>=2:

    denominations['2'] = amount//2

    amount= amount%2    

if amount>=1:

    denominations['1'] = amount//1


for key, val in denominations.items():

    print(f"{key}: {val}")



Enter an amount: 523

Total number of notes/coins=

500: 1

20: 1

2: 1

1: 1

如果使用如下所示的簡單邏輯,則可以減少代碼行數(shù),


def find_denominations():

    

    amount=int(input('Enter an amount: '))

    

    denominations = dict()

    

    DENOMINATIONS = [1000, 500, 100, 50, 20, 10, 5, 2, 1]

       

    print('Total number of notes/coins=')

    

    for d in DENOMINATIONS:

        if amount >= d:

            denominations[d] = amount // d

            amount %= d


    for key, val in denominations.items():

        print(f"{key}: {val}") 


查看完整回答
反對(duì) 回復(fù) 2023-10-11
?
倚天杖

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

Sreerams 的類似實(shí)現(xiàn)使用 while 循環(huán)而不是 for 循環(huán):


amount = int(input("Enter an amount: "))


counter = amount

pos = 0

notes = [1000, 500, 100, 50, 20, 10, 5, 2, 1]

output = []


while counter > 0:


    remainder = counter % notes[pos]

    sub = counter - remainder

    num = int(sub / notes[pos])

    counter -= sub

    output.append({notes[pos]: num})

    pos += 1


print("Total number of notes/coins=")


for r in output:

    for k,v in r.items():

        if v > 0:

            print("{}: {}".format(k, v))

請(qǐng)注意,Sreerams 代碼優(yōu)于我的代碼,它更易于閱讀,并且在規(guī)模上性能更高。


查看完整回答
反對(duì) 回復(fù) 2023-10-11
  • 3 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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