Python钱包列表功能

时间:2019-03-31 04:02:32

标签: python list currency

自从星期四以来,我一直在努力解决CS 101班的硬件问题。

我可以得到canAfford函数的点点滴滴,但是显示在错误的列下,并且我无法使所有内容完全正确。任何帮助表示赞赏。

def canAfford(wallet, amount):
    """ Returns True if the wallet has enough money for the amount"""
    pass
    # Add your code here

    """ 
    Logic for this function (You are free to use this as a guide if you want)
    sort the wallet from smallest to largest
    loop through the "coins" in the wallet while smallest coin < amount
        (pay with the smallest coins first until the smallest coin
        is larger than the amount left to pay)
        remove/pop the coin from the wallet (give to the cashier)
        subtract coin value from amount 
    if there is still an amount to remove from the wallet (amount > 0)
        (the smallest coin will be > amount)
        remove the smallest coin (give to cashier)
        determine what change the cashier will give back to add to your 
wallet
        let variable moneyToAdd = smallest coin - amount
        add coins to the wallet that represent moneyToAdd (refill your 
wallet)
    return the wallet
    """





def displayWallet(wallet):
    """ Displays the current coins in a wallet - Dont change """
    print('{:>10}{:>10}{:>10}{:>10}{:>10} : {:>5} 
cents'.format(wallet.count(1), 
                      wallet.count(5), wallet.count(10), 
wallet.count(25), 
                      wallet.count(100), sum(wallet)))
    pass     

#=============================================== ============================     #主要代码-请勿更改此代码

# set wallet with 1 dollar
wallet = [100]

# Display headers for wallet change
print('{:>10}{:>10}{:>10}{:>10}{:>10} : {:>5}'.format('Cents', 
'Nickels', 'Dimes', 'Quarters', 'Dollars', 'total'))

displayWallet(wallet)

amountOfItem = 13
while (canAfford(wallet, amountOfItem)):
    wallet = buyItem(wallet, amountOfItem)
    displayWallet(wallet)

print('remaining wallet')
displayWallet(wallet)

"""
Instructions:
- You will implement a wallet as a Python list.
- This wallet will contain change.
- For this program:
     a penny will be represented as 1
     a nickel as 5
     a dime as 10, 
     a quarter as 25
     a dollar as 100
- You will be implementing the function buyItem() that will take the 
  current wallet and the purchase amount for an item.  You will
  make the correct change for the cost of the item with the
  change in the wallet.
- The program will continue to buy an item of 13 cents while there 
  is enough money in the wallet to purchase that item.
- Review how floor divide (//) and modulus (%) work with numbers.
Sample Output:
     Cents   Nickels     Dimes  Quarters   Dollars : total
         0         0         0         0         1 :   100 cents
         2         0         1         3         0 :    87 cents
         4         0         2         2         0 :    74 cents
         1         0         1         2         0 :    61 cents
         3         0         2         1         0 :    48 cents
         0         0         1         1         0 :    35 cents
         2         0         2         0         0 :    22 cents
         4         1         0         0         0 :     9 cents
remaining wallet
         4         1         0         0         0 :     9 cents
"""

这是我到目前为止能得到的一切

Sample Output:
     Cents   Nickels     Dimes  Quarters   Dollars : total
         0         0         0         0         0 :   100 cents
         0         2         0         1         3 :    87 cents
         0         4         0         2         2 :    74 cents
         0         1         0         1         2 :    61 cents
         0         3         0         2         1 :    48 cents
         0         0         0         1         1 :    35 cents
         0         2         0         2         0 :    22 cents
         0         4         1         0         0 :     9 cents
remaining wallet
         0         0         0         0         0 :   100 cents

0 个答案:

没有答案