python - 无限循环开始了,为什么?

标签 python compiler-errors

我正在使用以下代码在Python中查找数字总和,但是在运行程序时开始无限循环

def digit_sum(n):
    k=str(n)
    i=0
    while i<range(len(k)):
        l=int(i)
        j=0
        j=j+i
        print j
        i+=1

digit_sum(1234)

最佳答案

您有缩进错误。

因此,要获得正确的输出,请尝试这种方式。您可以检查此代码here

def digit_sum(n):
    k = str(n)
    i = 0
    j = 0
    while i < len(k):
        l = int(k[i])
        j = j + l
        i += 1
    print j

digit_sum(1234)

关于python - 无限循环开始了,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45768114/

相关文章:

c# - 除非明确定义,否则System.Action在单元测试中不知道

python - MATLAB 多维矩阵到 NumPy 矩阵的转换

python - Beautiful Soup .text 和标签仍然附加

c++ - 尽管使用不存在的成员程序编译

c# - 属性规范中奇怪的歧义(两个 using 指令)

java - 从方法返回的StretchIcon类不起作用。导致错误

python - 有效地枚举 networkx 中 DiGraph 的所有简单路径

python - 检测数据帧是否具有 MultiIndex

jquery - Ansible - 组合两个变量将 [u...] 添加到其中之一

c++ - 为 std::string 释放内存的异常(尝试在 UE4 中使用 YOLO/Darknet)