缩进预期? [英] Indent Expected?

查看:224
本文介绍了缩进预期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,正在进行一次小型文本冒险,一直进行到现在为止.我目前正在实施一种剑系统,在这种系统中,如果您有一定大小的剑,则可以杀死某些大小的怪物.我正在尝试编写另一个怪物遇到的代码,并且已经编写了剑的东西,但是我试图使用elseif...elif...elif语句来完成它,即使我在正确的缩进中它仍然表示缩进预计我不知道该怎么办,下面是代码:

I'm sort of new to python and working on a small text adventure it's been going well until now I'm currently implementing a sword system where if you have a certain size sword you can slay certain size monsters. I'm trying to code another monster encounter and I have coded the sword stuff but I'm trying to finish it off with an else to the if...elif...elif statement and even though I have it in the right indentation it still says indent expected I don't know what to do here's the code:

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:

推荐答案

事实上,关于Python的缩进,您需要了解很多事情:

There are, in fact, multiple things you need to know about indentation in Python:

在其他语言中,缩进不是必需的,仅用于提高可读性.在Python中,缩进是必需的,它可以替换其他语言的关键字begin / end{ }.

In other languages, indention is not necessary but only serves to improve the readability. In Python, indentation is necessary and replaces the keywords begin / end or { } of other languages.

在执行代码之前已对此进行了验证.因此,即使永远不会到达带有缩进错误的代码,它也不会起作用.

This is verified before the execution of the code. Therefore even if the code with the indentation error is never reached, it won't work.

1. IndentationError: expected an indented block

1. IndentationError: expected an indented block

为什么会有这样的错误,但常见的原因是:

There are multiple reasons why you can have such an error, but the common reason will be:

  • 您有一个:,下面没有缩进的块.
  • You have a : without an indented block underneath.

这里有两个例子:

示例1,没有缩进块:

Example 1, no indented block:

输入:

if 3 != 4:
    print("usual")
else:

输出:

  File "<stdin>", line 4

    ^
IndentationError: expected an indented block

输出表明您需要在else:语句之后的第4行上有一个缩进块.

The output states that you need to have an indented block on line 4, after the else: statement.

示例2,不缩排:

Example 2, unindented block:

输入:

if 3 != 4:
print("usual")

输出

  File "<stdin>", line 2
    print("usual")
        ^
IndentationError: expected an indented block

输出表明您需要在if 3 != 4:语句之后的第2行上有一个缩进块.

The output states that you need to have an indented block on line 2, after the if 3 != 4: statement.

2. IndentationError: unexpected indent

2. IndentationError: unexpected indent

缩进块很重要,但仅应缩进块.此错误说:

It is important to indent blocks, but only blocks that should be indented. This error says:

-您有一个缩进的块,前面没有:.

- You have an indented block without a : before it.

示例:

Example:

输入:

a = 3
  a += 3

输出:

  File "<stdin>", line 2
    a += 3
    ^
IndentationError: unexpected indent

输出表明在第2行上没有出现缩进块.您应该通过删除缩进来解决此问题.

The output states that it wasn't expecting an indented block on line 2. You should fix this by remove the indent.

3. TabError: inconsistent use of tabs and spaces in indentation

3. TabError: inconsistent use of tabs and spaces in indentation

  • 但是基本上,您在代码中使用制表符和空格.
  • 你不想那样.
  • 删除所有选项卡,并将其替换为四个空格.
  • 并配置您的编辑器以自动执行此操作.
  • 您可以在此处.
  • But basically it's, you are using tabs and spaces in your code.
  • You don't want that.
  • Remove all tabs and replaces them by four spaces.
  • And configure your editor to do that automatically.
  • You can get some more info here.


最终,回到您的问题上:


Eventually, to come back on your problem:

我把它放在正确的缩进中,它仍然说缩进是我不知道该怎么做

I have it in the right indentation it still says indent expected I don't know what to do

只需查看错误的行号,然后使用先前的信息进行修复即可.

Just look at the line number of the error, and fix it using the previous information.

这篇关于缩进预期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆