谁能给我解释一下 python 中零钱和零钱的计算方法?

Can someone please explain to me the way to calculate the number of bills and coins of change in python?

所以我知道要计算找零后返还的纸币和硬币的 数量(例如: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 我怀疑是因为这个 %

我是编程的超级新手,我不能只是绕着它转!

% 不是模块;它被称为模数(或 "remainder")运算符。

对应整数除法:

9 == 4 * 2 + 1

9 // 4 == 2    # integer divison
9 % 4 == 1     # remainder

所以,例如:

# paying .51
x = 6351 // 1000      # == 6    maximum number of .00 bills
y = 6351 % 1000       # == 351  .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.