IndentationError:需要缩进块(JSON_Project.py,第22行)[重复]

问题描述 投票:0回答:1
def json_maker(angledeg,collangledeg):
for angledeg in angledeg_list:
    {
        for collangledeg in collangledeg_list:

point1 = "0,0,0"
point2 = str(L) + ",0,0"
point3 = str(L-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + ",0" 
point4 = str(-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + ",0"
point5 = "0,0," + str(-d)
point6 = str(L) + ",0," + str(-d)
point7 = str(L-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + "," + str(-d)
point8 = str(-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + "," + str(-d)

surface1 = [point2, point3, point7, point6]
surface2 = [point1, point5, point8, point4] 
surface3 = [point1, point2, point6, point5]
surface4 = [point1, point4, point3, point2] 
surface5 = [point5, point6, point7, point8]
surface6 = [point4, point8, point7, point3]

dict1 = {'emits': 'false', 'diffuse': 'true', 'e': emissivity_roof, "pnts": surface3} 
dict2 = {'emits': 'false', 'diffuse': 'false', 'e': emissivity_back_panel, "pnts": surface2}
dict3 = {'emits': 'false', 'diffuse': 'false', 'e': emissivity_back_panel, "pnts": surface1} 
dict4 = {'emits': 'false', 'e': 0.0, "pnts": surface4}
dict5 = {'emits': 'false', 'e': 0.0, "pnts": surface5}
dict6 = {'emits': 'true', 'collimate': str(-math.sin(collangle)) + ',' + str(-math.cos(collangle)) +',0', 'e': 1.0, "pnts": surface6}

surfaces.extend([dict1, dict2, dict3, dict4, dict5, dict6])

listentry = {'name': str(angledeg) + ' ' + str(collangledeg) , 'faces': surfaces}

outputdata.append(listentry)

with open('Inf_coll_spec_' + str(int(emissivity_roof*1000)) + '.json', 'w') as outfile:
    json.dump({ "traces" : outputdata }, outfile)
    
    result.list=[ angledeg, collangledeg, dict3, dict2, dict1, dict6]
        results.append(result.list)
        

        }

“IndentationError:需要一个缩进块(JSON_Project.py,第 22 行)”。谁能帮我消除这个错误并在 Python 中运行我的代码?然后我将在终端或命令提示符中运行此代码

python arrays for-loop math nested-loops
1个回答
1
投票

在Python中,代码块是通过标识(而不是{})组成的。 因此,您只需在每个 for 循环内的块上添加缩进即可。另外,删除 {}

def json_maker(angledeg,collangledeg):
    for angledeg in angledeg_list:
        
        for collangledeg in collangledeg_list:

            point1 = "0,0,0"
            point2 = str(L) + ",0,0"
            point3 = str(L-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + ",0" 
            point4 = str(-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + ",0"
            point5 = "0,0," + str(-d)
            point6 = str(L) + ",0," + str(-d)
            point7 = str(L-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + "," + str(-d)
            point8 = str(-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + "," + str(-d)

            surface1 = [point2, point3, point7, point6]
            surface2 = [point1, point5, point8, point4] 
            surface3 = [point1, point2, point6, point5]
            surface4 = [point1, point4, point3, point2] 
            surface5 = [point5, point6, point7, point8]
            surface6 = [point4, point8, point7, point3]

            dict1 = {'emits': 'false', 'diffuse': 'true', 'e': emissivity_roof, "pnts": surface3} 
            dict2 = {'emits': 'false', 'diffuse': 'false', 'e': emissivity_back_panel, "pnts": surface2}
            dict3 = {'emits': 'false', 'diffuse': 'false', 'e': emissivity_back_panel, "pnts": surface1} 
            dict4 = {'emits': 'false', 'e': 0.0, "pnts": surface4}
            dict5 = {'emits': 'false', 'e': 0.0, "pnts": surface5}
            dict6 = {'emits': 'true', 'collimate': str(-math.sin(collangle)) + ',' + str(-math.cos(collangle)) +',0', 'e': 1.0, "pnts": surface6}

            surfaces.extend([dict1, dict2, dict3, dict4, dict5, dict6])

            listentry = {'name': str(angledeg) + ' ' + str(collangledeg) , 'faces': surfaces}

            outputdata.append(listentry)

    with open('Inf_coll_spec_' + str(int(emissivity_roof*1000)) + '.json', 'w') as outfile:
        json.dump({ "traces" : outputdata }, outfile)

        result.list=[ angledeg, collangledeg, dict3, dict2, dict1, dict6]
        results.append(result.list)

例如,这部分位于函数内部并且都是 for 循环:

            point1 = "0,0,0"
            point2 = str(L) + ",0,0"
            point3 = str(L-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + ",0" 
            point4 = str(-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + ",0"
            point5 = "0,0," + str(-d)
            point6 = str(L) + ",0," + str(-d)
            point7 = str(L-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + "," + str(-d)
            point8 = str(-W*math.cos(angle)) + "," + str(W*math.sin(angle)) + "," + str(-d)

            surface1 = [point2, point3, point7, point6]
            surface2 = [point1, point5, point8, point4] 
            surface3 = [point1, point2, point6, point5]
            surface4 = [point1, point4, point3, point2] 
            surface5 = [point5, point6, point7, point8]
            surface6 = [point4, point8, point7, point3]

            dict1 = {'emits': 'false', 'diffuse': 'true', 'e': emissivity_roof, "pnts": surface3} 
            dict2 = {'emits': 'false', 'diffuse': 'false', 'e': emissivity_back_panel, "pnts": surface2}
            dict3 = {'emits': 'false', 'diffuse': 'false', 'e': emissivity_back_panel, "pnts": surface1} 
            dict4 = {'emits': 'false', 'e': 0.0, "pnts": surface4}
            dict5 = {'emits': 'false', 'e': 0.0, "pnts": surface5}
            dict6 = {'emits': 'true', 'collimate': str(-math.sin(collangle)) + ',' + str(-math.cos(collangle)) +',0', 'e': 1.0, "pnts": surface6}

            surfaces.extend([dict1, dict2, dict3, dict4, dict5, dict6])

            listentry = {'name': str(angledeg) + ' ' + str(collangledeg) , 'faces': surfaces}

            outputdata.append(listentry)

这部分就在函数内部:

    with open('Inf_coll_spec_' + str(int(emissivity_roof*1000)) + '.json', 'w') as outfile:
        json.dump({ "traces" : outputdata }, outfile)

        result.list=[ angledeg, collangledeg, dict3, dict2, dict1, dict6]
        results.append(result.list)
© www.soinside.com 2019 - 2024. All rights reserved.