Indent Expected? [duplicate]

2024/11/13 22:59:47

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 += 50print ('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 += 50print ('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 += 50print ('you now have ' + str(gold) + ' gold')else:
Answer

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 2print("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 = 3a += 3

Output:

  File "<stdin>", line 2a += 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://en.xdnf.cn/q/119105.html

Related Q&A

Convert QueryDict to key-value pair dictionary

I have a QueryDict that I get from request.POST in this format: <QueryDict: {name: [John], urls: [google.com/\r\nbing.com/\r\naskjeeves.com/], user_email: [[email protected]]}>Why are the values …

How to test items with each other in 2 dimensional list?

We have a 2 dimensional list (for this example we have it populated with unique 6 nodes and 3 masks)myList = [[node1, mask1], [node2, mask1], [node3, mask1], [node4, mask2], [node5, mask2], [node6, mas…

InsecureRequestWarning + MarkupResemblesLocatorWarning:

Id like to scrape a site for my office work. I am learning each day. I need your support guys. Here is the Code: url = https://www.eprocure.gov.bd/partner/ViewTenderPaymentDetails.jsp?payId=33767442&a…

Fetching images from URL and saving on server and/or Table (ImageField)

Im not seeing much documentation on this. Im trying to get an image uploaded onto server from a URL. Ideally Id like to make things simple but Im in two minds as to whether using an ImageField is the b…

Comparing list with a list of lists

I have a list string_array = [1, 2, 3, 4, 5, 6] and a list of lists multi_list = [[1, 2], [2, 3], [2, 4], [4, 5], [5, 6]]The first element of each sub-list in multi_list will have an associated entry …

Cannot save data to database Python

I have a table called category TABLES["category"] = ("""CREATE TABLE category (category_id INTEGER NOT NULL AUTO_INCREMENT,category_name VARCHAR(120) NOT NULL,PRIMARY KEY (cate…

How to generate a permutation of list of lists in python

I have a list of lists say[[2, 4, 6], [2, 6, 10], [2, 12, 22], [4, 6, 8], [4, 8, 12], [6, 8, 10], [8, 10, 12], [8, 15, 22], [10, 11, 12]]How do I generate a combination of the lists for a given length?…

Issue sending file via Discord bot (Python)

if message.content.upper().startswith("!HEADPATS"):time.sleep(1)with open(tenor.gif, rb) as picture:await client.send_file(channel, picture)Ive got my discord bot up and running (everythings …

Matplotlib installation on Mavericks

Im having problem while installing matplotlib. Im using Mavericks and it complains about a deprecated NumPy API both installing via pip and installing from source (following the instructions here https…

Exact string search in XML files?

I need to search into some XML files (all of them have the same name, pom.xml) for the following text sequence exactly (also in subfolders), so in case somebody write some text or even a blank, I must …