我正在编写一个简单的代码来计算应发生多少变化,并使用打印语句来检查我的值。
cost = float(raw_input('How much did your meal cost? > '))
payment = float(raw_input('How much did you pay? > '))
change = (payment - cost)
print change
if change < 0:
print 'You need to pay $%.2f more!' % (-change)
dollars, q, d, n, p = 0, 0, 0, 0, 0
change = float(change*100)
print change
if change> 100:
dollars = int(change/100)
change = change % 100
print change
if change >= 25:
q = int(change/25)
change = change % 25
print change
if change >= 10:
d = int(change/10)
change = change % 10
print change
if change >= 5:
n = int(change/5)
change = change % 5
print change
if change >= 1:
p = int(change/1)
else:
print "why doesn't this work?"
print p
print change
print 'you get %d dollars, %d quarters, %d dimes, %d nickels, and %d pennies!' % (dollars, q, d, n, p)
这将返回:
你的一顿饭花了多少钱?> 34.34
你付了多少钱?> 40
5.66
566.0
66.0
16.0
6.0
1.0
为什么这不起作用?
0
1.0
我可以在便士计算之前和之后看到 change = 1.0,但是
if change >= 1
仍然读为假,并转到 else 语句。这里发生了什么,我该如何解决?