因此,我知道要计算找零后退还的纸币和硬币的数量(例如:2张100美元的纸币),您需要使用该%
模块。
但是,为什么需要%
模块化,人们为什么不只是减去?
例如,我有100美元找零
我知道我必须将其更改为几美分,使其等于10000美分
cents = int(change*100) ---->10000cents
因此,当我计算要返还多少张$ 100的钞票,$ 50的钞票等时,为什么我需要计算%,为什么我需要除以?
前任: cents = change*100
hundered_dollars = int(cents /10000)
如果我在这里除,10000/10000
等于1,但是当我将print(hundered_dollars)
其打印为0时!
cents = cents %10000
我怀疑是因为这个%
我是编程的超级新手,我不能只把头缠住它!
%
不是模块;它称为模数(或“余数”)运算符。
它是整数除法的对应项:
9 == 4 * 2 + 1
9 // 4 == 2 # integer divison
9 % 4 == 1 # remainder
因此,例如:
# paying $63.51
x = 6351 // 1000 # == 6 maximum number of $10.00 bills
y = 6351 % 1000 # == 351 $3.51 not payable in 10s.
# you could instead do
y = 6351 - (6351 // 1000) * 1000
# this would give the same result,
# but you've got to admit it's a lot
# less readable.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句