Skip to main content
 首页 » 编程设计

python之嵌套 for 循环中的错误 (Python)

2025年05月04日9lautakyan007

我在以下代码中遇到错误。错误消息是 “错误:检测到不一致的缩进!”

s=[30,40,50] 
a=[5e6,6e6,7e6,8e6,8.5e6,9e6,10e6,12e6] 
p=[0.0,0.002,0.004,0.006,0.008,0.01,0.015,0.05,0.1,0.15,0.2] 
j=0 
b=0 
x=0 
 
 
for j in s: 
    h=s[j] 
    print "here is the first loop" +h 
    for b in a: 
           c=a[b]                                #too much indentation 
           print"here is the second loop" +c     #too much indentation 
           for x in p:                           #too much indentation 
                k=p[x] 
                print"here is the third loop" +k 

如果有任何其他错误,如果这里的任何人都可以纠正我,我将非常感激。

谢谢。

/吉拉尼

请您参考如下方法:

清理制表符和空格后(您应该只有制表符或只有空格),您需要修复循环:

s = [30,40,50] 
a = [5e6,6e6,7e6,8e6,8.5e6,9e6,10e6,12e6] 
p = [0.0,0.002,0.004,0.006,0.008,0.01,0.015,0.05,0.1,0.15,0.2] 
 
for j in s:        # within loop j is being 30 then 40 then 50, same true for other loops 
    print "here is the first loop" + j 
    for b in a: 
        print"here is the second loop" + b 
        for x in p: 
            print"here is the third loop" + x 

否则你会遇到IndexError。