【问题标题】:How to change this code to not printing unused coin=0?如何将此代码更改为不打印未使用的硬币=0?
【发布时间】:2021-01-20 05:12:27
【问题描述】:

我正在尝试构建一个功能,在人们输入金额后,它将显示他们需要的最少硬币或纸币数量。但这有什么方法可以让我改变它,使它不会打印未使用硬币的名称和编号? (作为初学者)感谢您的帮助! (能不能用for循环来处理?)

【问题讨论】:

    标签: python


    【解决方案1】:

    不要为每个恶魔保留一个变量,而是保留一个 dict 并根据所使用的面额更新 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
    

    如果您使用如下所示的简单逻辑,您可以减少代码行数,

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

    【讨论】:

    • 面额是一个字典,代码正在迭代字典并从字典中提取键和值并将其打印出来。
    【解决方案2】:

    使用 while 循环而不是 for 循环的 Sreerams 类似实现:

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

    请注意,Sreerams 的代码比我的要好,它更易于阅读,并且在规模上会更高效。

    【讨论】:

      【解决方案3】:

      循环可用于遍历笔记列表并在循环内,如果发现任何笔记被计数,则可以打印。

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-19
        • 2019-11-21
        • 2017-02-28
        • 1970-01-01
        • 2017-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多