我正在使用Python,并且我试图将美分的一定数量的钱转换为宿舍,镍,硬币和便士中的等价物。使用Python将美分转换为宿舍,镍币,硬币和便士
这是我到目前为止,但我看到的问题是,我不知道如何从宿舍拿走余下的钱,并将其分解为硬币,镍和便士。我对此很陌生,只是很难过。我并不是要求某人解决问题,只是指出我做错了什么(也许我需要做些什么才能解决问题)。
# Convert some money to an appropriate collection of cents
penny = 1
nickel = 5
dime = 10
quarter = 25
quarters = 0
dimes = 0
nickels = 0
pennys = 0
cents = int(input("Please enter an amount of money you have in cents: "))
if cents >= 25:
quarters = cents/quarter
cents % quarter
if cents >= 10:
dimes = cents/dime
cents % dime
if cents >= 5:
nickels = cents /nickel
cents % nickel
if cents > 0:
pennys = cents/penny
cents = 0
print ("The coins are: quarters", quarters,\
",dimes", dimes, ",nickels", nickels, ", and pennys.", pennys)
您计算了'cents%quarter',但没有将它分配给下一个语句的变量。基于你有什么可以做'美分=美分%季度'。同样的'美分%'陈述的其余部分。 – metatoaster
你也可以为此使用'divmod'。 –