Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使函数打印更多结果?_Python_Tuples_Divmod - Fatal编程技术网

Python 如何使函数打印更多结果?

Python 如何使函数打印更多结果?,python,tuples,divmod,Python,Tuples,Divmod,所以我已经花了几个小时来做这个测试,我想用一个元组和divmod函数来产生一个非常串联的结果,最后我找到了一个解决方案。。 但我不明白为什么它只打印一个结果而不是多个。。 这是我的密码: def tilt(money): amount = [(1,'dollar'),(0.25,'quarter'),(0.10,'dime'),(0.05,'nickel'),(0.01,'pennie')] total = {} for amount_value, amount_n

所以我已经花了几个小时来做这个测试,我想用一个元组和divmod函数来产生一个非常串联的结果,最后我找到了一个解决方案。。 但我不明白为什么它只打印一个结果而不是多个。。 这是我的密码:

def tilt(money):
     amount = [(1,'dollar'),(0.25,'quarter'),(0.10,'dime'),(0.05,'nickel'),(0.01,'pennie')]
     total = {}
     for amount_value, amount_name in amount:
         if money >= amount_value:
             number_amount, money = divmod(money, amount_value)
             total[amount_name] = number_amount
     return total

    print(tilt(10.65))

# result for this is {'dollar': 10.0}

# what I expect is {'dollar': 10, 'quarter': 2, 'dime': 1, 'nickel': 1}
我只是不能把我的头绕在一个解决方案上,我想避免使用没完没了的If-Else代码来达到这一点。
我肯定有一个简单的解决方案我不知道,提前谢谢你的帮助。

我不能评论;但这可能取决于你的压痕。如果您将return语句与for循环对齐,您将得到所需的响应

因此,它如下所示:

def tilt(money):
    amount = [(1, 'dollar'), (0.25, 'quarter'), (0.10, 'dime'),
              (0.05, 'nickel'), (0.01, 'pennie')]
    total = {}
    for amount_value, amount_name in amount:
        if money >= amount_value:
            number_amount, money = divmod(money, amount_value)
            total[amount_name] = number_amount
    return total

请用正确的缩进更新您的问题。Python和Python程序员一样对缩进非常敏感。很抱歉,我刚刚更新了它,它更好吗?不,这段代码也会因缩进错误而失败。看来您需要重复一个关于dicts的教程,以学习如何添加新条目。很抱歉,我的错误,我还在学习。我应该删除这个吗?天哪,我挠头这么久了,我一直都是对的!非常感谢!看起来你用
tilt(1.43)
float可能不是最好的选择。缩进在python中非常重要-如果你刚开始使用python,在缩进代码时要格外小心。是的,我刚刚意识到我少了一分钱,我不明白为什么…请看