为什么返回Int?蟒蛇

时间:2020-05-29 13:49:31

标签: python

在此视频中(7分30秒),我从该视频中复制的以下代码返回3,尽管 我的回报为4.04。 我不明白为什么视频中的代码返回Int,而我的返回Float。

https://www.youtube.com/watch?v=HWW-jA6YjHk&list=UUNc-Wa_ZNBAGzFkYbAHw9eg&index=29

def num_coins(cents):
    if cents < 1:
        return 0
    coins = [25, 10, 5, 1]
    num_of_coins = 0
    for coin in coins:
        num_of_coins += cents / coin
        cents = cents % coin
        if cents == 0:
            break
    return num_of_coins

print(num_coins(31))

1 个答案:

答案 0 :(得分:-1)

使用它来获得正确的答案:

def num_coins(cents):
    if cents < 1:
        return 0
    coins = [25, 10, 5, 1]
    num_of_coins = 0
    for coin in coins:
        num_of_coins += int(cents / coin)
        cents = cents % coin
        if cents == 0:
            break
    return num_of_coins

print(num_coins(31))

/运算符与python 2和python 3相似

相关问题