我在 View 代码中收到来自 django
的类型错误。
尝试四舍五入到最接近的 5。即 7 变成 10 或 123 变成 125,等等
我有一个整数列表
valList = [ int(i) for i in str(int(math.ceil(result))) ]
然后我改变一个数字
valList[len(valList)-1] = 5
然后组合这些值再次创建一个整数。
result = ''.join(map(str, valList))
然后转换回一个整数
result = int(result)
当我使用 python 控制台(以及在 django shell 中)时,此方法有效,但是当我运行这是我的 django View 时,我得到一个 TypeError Exception
错误是 map() takes恰好 1 个参数(给定 2 个)
我不知道是什么原因造成的......或者我做错了什么。
谢谢。
附注如果有人知道四舍五入到最接近的 5 的优雅解决方案,我会洗耳恭听。
最佳答案
数学解决方案如何:
>>> def f(n):
... q, r = divmod(n,5)
... if r == 0:
... return q * 5
... else:
... return (q * 5) + 5
...
>>> f(7)
10
>>> f(123)
125
>>> f(120)
120
关于python - Django - 我如何错误地使用 python 映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7023009/