Indent Expected?

久未见 提交于 2019-12-08 14:13:42

问题


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:

回答1:


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

Python really cares about indention.

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.

There are different indention errors and you reading them helps a lot:

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.

Here are two examples:

Example 1, no indented block:

Input:

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

Output:

  File "<stdin>", line 4

    ^
IndentationError: expected an indented block

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

Example 2, unindented block:

Input:

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

Output

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

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

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:

Input:

a = 3
  a += 3

Output:

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

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

  • 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.



来源:https://stackoverflow.com/questions/39395226/indent-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!