擅长:python、mysql、java
<p>好吧,我假设您正在尝试使用多种类型的票据来计算交易的变化。你知道吗</p>
<p>问题是,你需要保持一个连续的记录有多少变化,你还剩支付。我用<code>num_curr_bill</code>来计算您当前支付的账单类型有多少,而您的<code>hef</code>我改成了<code>remaining_change</code>(所以这对我来说很有意义)来支付剩余的更改。你知道吗</p>
<pre><code>a= input('price: ')
b= input('cash paid: ')
coin_bills=[100,50,20,10,5,1,0.5]
if b>=a:
# Calculate total change to pay out, ONCE (so not in the loop)
remaining_change = b-a
for i in coin_bills:
# Find the number of the current bill to pay out
num_curr_bill = remaining_change/i
# Subtract how much you paid out with the current bill from the remaining change
remaining_change -= num_curr_bill * i
# Print the result for the current bill.
print num_curr_bill,'*',i
else:
print 'pay up!'
</code></pre>
<p>因此,如果价格为120,支付的现金为175,则输出为:</p>
<pre><code>price: 120
cash paid: 175
0 * 100
1 * 50
0 * 20
0 * 10
1 * 5
0 * 1
0.0 * 0.5
</code></pre>
<p>一张50英镑的钞票和一张5英镑的钞票加起来就是55英镑,这是正确的零钱。你知道吗</p>
<p>编辑:在我自己的代码中,我会更谨慎地使用注释,但我在这里添加注释是为了解释,这样您就可以更清楚地看到我的思维过程是什么。你知道吗</p>
<p>编辑2:我会考虑去掉硬币纸币中的0.5,用1.0代替1,因为任何零碎的金额最终都是0.5的零碎。你知道吗</p>