[Python]:IndentationError: unexpected indent

本文详细解析了Python中IndentationError的含义,并提供了修复缩进错误的方法实例,包括直接传递参数、使用变量和混合数学运算。通过实例学习如何避免意外缩进导致的问题。

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

python代码报错IndentationError: unexpected indent

缩进错误:意外缩进

Indentation [ˌɪndenˈteɪʃn]  n. 缺口;凹陷;凹痕;行首缩进;行首空格;造成凹陷(或缺口);将行首缩进

indent  [ɪnˈdent , ˈɪndent]  v. 将(印刷或书写的行)缩进,缩格,缩排    n. 订单;订购

unexpected  [ˌʌnɪkˈspektɪd]  adj. 出乎意料的;始料不及的

解决办法:

检查缩进,该缩进的缩进,不该缩进的绝对不要缩进!

def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print "You have %d cheeses!" % cheese_count
    print "You have %d boxes of crackers!" % boxes_of_crackers
    print "Man that's enough for a party!"
    print "Get a blanket.\n"
	
print "We can just give the function numbers directly:"
cheese_and_crackers(20,30)//执行的方法一定不能缩进,如果缩进了,结果就是什么都不执行。
	
	
print "OR, we can use variables from our script:"
amount_of_cheese = 10
amount_of_crackers = 50
	
cheese_and_crackers(amount_of_cheese,amount_of_crackers)
	
	
print "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)

	
print "And we can combine the two, variables and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值