persistence - 陷入 CodeWars 问题.. 持久性错误问题
问题描述
我试图解决这个问题的代码是:
def persistence(n):
count = 0
multi = 1
while n > 9:
string = str(n)
for i in string:
operator = int(i)
multi *= operator
count += 1
n = multi
return count
它通过了持久性(4)= 0,持久性(25)= 2,持久性(999)= 4的示例测试,但无法通过这个示例:持久性(39)= 3,我的代码给了我4而不是3 .我无法弄清楚示例输入 39 发生了什么。有人可以解释一下我的代码有什么错误吗?
解决方案
由于我第一眼也看不到,所以我在pythontutor.com
. 您的代码中唯一缺少的部分是在 while 循环结束时将您的“multi”变量重置为 1。
def persistence(n):
count = 0
multi = 1
while n > 9:
string = str(n)
for i in string:
operator = int(i)
multi *= operator
count += 1
n = multi
multi = 1
return count
推荐阅读
- excel-formula - 如何识别一个值是否介于数组中的值之间并为整个数组重复 - Excel
- typescript - TypeError:AWS.SecretsManager 不是构造函数
- python - 在 django 中实现通道后,不同的请求相互混淆了响应(windows OS)
- azure-devops - 如何从 Azure DevOps 将文件复制到 Azure Windows VM?不使用 VM 凭据
- search - 是否可以在 MS Search 中隐藏自定义垂直?
- android - 受信任的 Web 活动:无法删除 URL 标头
- c++ - 在 esp-32 上使用 micropython 运行 c++ 程序
- pandas - 使用 .at 方法从数据帧访问标量
- 3d - 从相机上的 2D 点计算 3D 点及其与相机的距离
- android - 在颤动中禁用移动位置