当我编译下面的Python代码时,我得到IndentationError:unindent不匹配任何外部缩进级别#main module
def main():
# Local variables
weight = 0.0
shipping = 0.0
# Get package weight
weight = int(input("Enter the weight of the package: "))
# Calculate the shipping charge
if weight < 2:
shipping_charge = '$1.10'
elif weight > 2 and weight < 6:
shipping_charge = '$2.20'
elif weight > 6 and weight < 10:
shipping_charge = '$3.70'
elif weight > 10:
shipping_charge = '$3.80'
# print shipping charge
showShipping(shipping)
print("Shipping charge:", shipping_charge)
为什么?
1 回答
重新缩进所有内容并确保已将选项卡转换为空格 . 然后确保所有“空格”都处于适当的缩进级别 . 差异可能会导致此类错误消息 .
在python中,给定块内的所有内容必须处于相同的缩进级别,无论是4或5还是(在此处插入个人喜欢的空格数) .
这是一个很好的参考:Python docs on indentation
注意:根据您在SO上发布的内容,最后一个print语句似乎比其余语句缩进 . 这应该是在完成上述操作后首先要仔细检查的事情 .