我不理解我的任务:
编写一个Python程序,提示用户输入成本 一个项目,并提示用户输入以美分支付的金额。 将更改打印为四分之一,硬币和镍币的数量 返回(假设价格是5美分的倍数)。
这是我的代码:
i = int(input('how much did this item cost'))
p = int(input('how much did i pay'))
e = p-i
q = e/25
q = e%25
d = q/10
d = q%10``
n = d/5
n = d%5
print (' q = ',q,'\n d = ',d,'\n n = ',n)
答案 0 :(得分:1)
这是使用除法的另一种解决方案。
import math
change = 50
quarterDif = change / 25
change -= math.floor(quarterDif) * 25
dimeDif = change / 10
change -= math.floor(dimeDif) * 10
nickelDif = change / 5
change -= math.floor(nickelDif) * 5
print(str(quarterDif) + str(dimeDif) + str(nickelDif))
答案 1 :(得分:0)
我要做的是说,在达到极限之前,我可以使用多少个季度?然后,为硬币和镍币做同样的事情。
i = int(input('how much did this item cost'))
p = int(input('how much did i pay'))
change = p - i
tempVal = 0
quartersUsed = 0
while tempVal <= change:
tempVal += 25
quartersUsed += 1
tempVal -= 25
quartersUsed -= 1
change -= tempVal
tempVal = 0
dimesUsed = 0
while tempVal <= change:
tempVal += 10
dimesUsed += 1
tempVal -= 10
dimesUsed -= 1
change -= tempVal
tempVal = 0
nickelsUsed = 0
while tempVal <= change:
tempVal += 5
nickelsUsed += 1
tempVal -= 5
nickelsUsed -= 1
change -= tempVal
# change SHOULD = 0 now
print(str(quartersUsed) + " " + str(dimesUsed) + " " + str(nickelsUsed))
这应该有效。用一些值测试它并且对我的知识很有效。 希望这会有所帮助。
在问这个问题之前,你似乎没有多想过来解决这个问题。你甚至没有排除是否使用除法或模数(我用过的都不是)。但是,在发布此处之前,请尝试使用您的头部。