Python代码IndentationError错误修复及运行求助
Fixing IndentationError & Other Issues in Your JSON Maker Function
Hey there! Let's get your code up and running smoothly. First, that IndentationError is happening because all code inside your json_maker function needs to be indented—Python uses indentation to define code blocks, so every line after def json_maker(...) must be shifted right (we use 4 spaces as standard).
Beyond the indentation, there are a few missing pieces: undefined variables, uninitialized lists, and a reference to an undefined result object. Here's the fully fixed version with clear explanations:
Fixed Code
import math import json def json_maker(angledeg, collangledeg): # Initialize empty lists to store surfaces and output data surfaces = [] outputdata = [] results = [] # Define your constants (adjust these values to match your actual requirements) L = 10.0 # Example length value W = 2.0 # Example width value d = 1.5 # Example depth value emissivity_roof = 0.85 emissivity_back_panel = 0.1 # Convert degrees to radians (math module functions use radians, not degrees) angle = math.radians(angledeg) collangle = math.radians(collangledeg) # Your original logic, now properly indented 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': f"{angledeg} {collangledeg}" , 'faces': surfaces} outputdata.append(listentry) # Write JSON output to file with open(f'Inf_coll_spec_{int(emissivity_roof*1000)}.json', 'w') as outfile: json.dump({ "traces" : outputdata }, outfile) # Replace undefined 'result' object with a direct list creation result_list = [angledeg, collangledeg, dict3, dict2, dict1, dict6] results.append(result_list) # Optional: return results if you need to use them outside the function return results # Example usage to test the function if __name__ == "__main__": json_maker(30, 45)
Key Fixes Breakdown:
- Indentation: All code inside
json_makeris now indented with 4 spaces, which resolves theIndentationErrorimmediately. - Module Imports: Added
import mathandimport jsonsince your code relies on these libraries for calculations and JSON handling. - List Initialization: Created empty
surfaces,outputdata, andresultslists—they were referenced in your original code but never defined. - Constant Values: Added example values for
L,W,d, and emissivity constants—adjust these to match your actual project requirements. - Radian Conversion: Converted input degrees to radians, since Python's
math.sin/math.cosfunctions require radians instead of degrees. - Undefined
resultObject: Replaced the uninitializedresult.listwith a direct list (result_list), asresultwas never declared in your original code. - Readable String Formatting: Used f-strings for cleaner, more readable string construction (optional but improves code maintainability).
To run this, save it as JSON_Project.py and execute it in your terminal/command prompt with:
python JSON_Project.py
内容的提问来源于stack exchange,提问作者Vibhu Sharma
相关产品推荐
相关产品推荐

