我是python的新手,正在进行一次小文本冒险,它一直进展顺利,直到现在我正在实施剑系统,如果你有一把剑,你可以杀死一定大小的怪物。我试图编写另一个怪物遭遇,我编写了剑的东西,但我试图用else
if...elif...elif
声明完成它,即使我有它在正确的缩进中它仍然说缩进,我不知道该怎么做这里的代码:
print ('you find a monster about 3/4 your size do you attack? Y/N')
yesnotwo=input()
if yesnotwo == 'Y':
if ssword == 'Y':
print ('armed with a small sword you charge the monster, you impale it before it can attack it has 50 gold')
gold += 50
print ('you now have ' + str(gold) + ' gold')
elif msword == 'Y':
print ('armed with a medium sword you charge the monster, you impale the monster before it can attack it has 50 gold')
gold += 50
print ('you now have ' + str(gold) + ' gold')
elif lsword == 'Y':
print ('armed with a large broadsword you charge the beast splitting it in half before it can attack you find 50 gold ')
gold += 50
print ('you now have ' + str(gold) + ' gold')
else:
答案 0 :(得分:6)
事实上,你需要了解Python中缩进的多项内容:
在许多其他语言中,缩进不是必需的,但提高了可读性。在Python缩进中,替换关键字begin / end
或{ }
,因此是必要的。
这是在执行代码之前验证的,因此即使具有缩进错误的代码永远不会到达,它也不会起作用。
<强> 1。 &#34; IndentationError:预期缩进块&#34;
它们是导致此类错误的多种原因,但常见原因是:
以下是两个例子:
示例1,没有缩进块:
输入:
if 3 != 4:
print("usual")
else:
输出:
File "<stdin>", line 4
^
IndentationError: expected an indented block
输出表明您需要在else:
语句之后有一个缩进的第4行
示例2,未缩进的块:
输入:
if 3 != 4:
print("usual")
输出
File "<stdin>", line 2
print("usual")
^
IndentationError: expected an indented block
输出表明您需要在if 3 != 4:
语句之后有一个缩进的第2行
<强> 2。 &#34; IndentationError:意外缩进&#34;
缩进块非常重要,但只能缩进块。 所以基本上这个错误说:
- 你有一个没有&#34;的缩进块:&#34;在它之前。
<强> 实施例 强>
输入:
a = 3
a += 3
输出:
File "<stdin>", line 2
a += 3
^
IndentationError: unexpected indent
输出表明他没有预料到缩进块行2,那么你应该删除它。
第3。 &#34; TabError:缩进中标签和空格的使用不一致&#34;
<小时/> 最后,回到你的问题:
我有正确的缩进它仍然说缩进预期我不知道该怎么做
只需查看错误的行号,然后使用以前的信息进行修复。