Python脚本运行出现语法错误:IndentationError:unexpected indent

本文分享了在Python编程中遇到IndentationError错误的解决经验,详细介绍了如何检查和修正代码缩进问题,包括使用文本编辑器Notepad++查看特殊字符的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 对于py来说典型错误就是缩进,,烦不胜烦,整理一下解决方法:
一个python脚本,本来都运行好好的,然后写了几行代码,而且也都确保每行都对齐了,但是运行的时候,却出现语法错误: 
IndentationError: unexpected indent

【解决过程】 
1.对于此错误,最常见的原因是,的确没有对齐。但是我根据错误提示的行数,去代码中看了下,没啥问题啊。 
都是用TAB键,对齐好了的,没有不对齐的行数啊。 
2.以为是前面的注释的内容影响后面的语句的语法了,所以把前面的注释也删除了。 
结果还是此语法错误。 
3.后来折腾了半天,突然想到了,把当前python脚本的所有字符都显示出来看看有没有啥特殊的字符。

当前用的文本编辑器Notepad++,好像有个设置,可以显示所有的字符的。 

转载于:https://www.cnblogs.com/fgxwan/p/9645953.html

if not mat.use_nodes or not mat.node_tree: ... continue ... >>> # 跳过只读材质[^2] >>> if mat.library or mat.users == 0: File "<blender_console>", line 1 if mat.library or mat.users == 0: IndentationError: unexpected indent >>> continue File "<blender_console>", line 1 continue IndentationError: unexpected indent >>> >>> nodes = mat.node_tree.nodes File "<blender_console>", line 1 nodes = mat.node_tree.nodes IndentationError: unexpected indent >>> links = mat.node_tree.links File "<blender_console>", line 1 links = mat.node_tree.links IndentationError: unexpected indent >>> >>> # 查找原理化BSDF节点 >>> bsdf_node = None File "<blender_console>", line 1 bsdf_node = None IndentationError: unexpected indent >>> for node in nodes: File "<blender_console>", line 1 for node in nodes: IndentationError: unexpected indent >>> if node.type in {'BSDF_PRINCIPLED', 'PRINCIPLED_BSDF'}: File "<blender_console>", line 1 if node.type in {'BSDF_PRINCIPLED', 'PRINCIPLED_BSDF'}: IndentationError: unexpected indent >>> bsdf_node = node File "<blender_console>", line 1 bsdf_node = node IndentationError: unexpected indent >>> break File "<blender_console>", line 1 break IndentationError: unexpected indent >>> >>> if not bsdf_node: File "<blender_console>", line 1 if not bsdf_node: IndentationError: unexpected indent >>> continue File "<blender_console>", line 1 continue IndentationError: unexpected indent>>> >>> # 处理法线输入 >>> normal_input = bsdf_node.inputs.get('Normal') File "<blender_console>", line 1 normal_input = bsdf_node.inputs.get('Normal') IndentationError: unexpected indent >>> if normal_input: File "<blender_console>", line 1 if normal_input: IndentationError: unexpected indent >>> # 1. 断开所有连接 >>> for link in list(normal_input.links): File "<blender_console>", line 1 for link in list(normal_input.links): IndentationError: unexpected indent >>> links.remove(link) File "<blender_console>", line 1 links.remove(link) IndentationError: unexpected indent >>> >>> # 2. 重置为默认值 >>> normal_input.default_value = (0.0, 0.0, 1.0) # 世界空间法线方向 File "<blender_console>", line 1 normal_input.default_value = (0.0, 0.0, 1.0) # 世界空间法线方向 IndentationError: unexpected indent >>> >>> # 3. 可选:添加法线贴图节点并设置默认值 >>> normal_map = nodes.new(type='ShaderNodeNormalMap') File "<blender_console>", line 1 normal_map = nodes.new(type='ShaderNodeNormalMap') IndentationError: unexpected indent >>> normal_map.location = (bsdf_node.location.x - 300, bsdf_node.location.y) File "<blender_console>", line 1 normal_map.location = (bsdf_node.location.x - 300, bsdf_node.location.y) IndentationError: unexpected indent >>> normal_map.inputs['Strength'].default_value = 1.0 # 默认强度 File "<blender_console>", line 1 normal_map.inputs['Strength'].default_value = 1.0 # 默认强度 IndentationError: unexpected indent >>> >>> # 连接法线贴图节点 >>> links.new(normal_map.outputs['Normal'], normal_input) File "<blender_console>", line 1 links.new(normal_map.outputs['Normal'], normal_input) IndentationError: unexpected indent >>> >>> # 执行函数 >>> reset_all_normal_connections() >>>
最新发布
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值