Skip to content
Advertisement

IndentationError In Python Script [closed]

So I’m making a text adventure game. When I run the code, it says “IndentationError: unindent does not match any outer indentation level on line 24 elif response == "right": ^ in main.py

Can someone tell me what’s wrong with the code? This is the code:

import os

# Variables
directions = ["left", "right", "forward", "backward"]

# Name
name = raw_input("What is your name, camper? ")
os.system("clear")

# Story Introduction
print ("""
It's the last night of RV camping with your family in the big forest.
Your family has decided to go on a walk to see the nice stars in the night sky.
As you follow your family, you drop your flashlight in the bushes.
When you go back to the path, you're completly turned around and you have no clue where your family went.n
""")
print ("When you return to the path, it splits! Which way do you go " + name + "?")
os.system("clear")

while response not in directions:
  response = raw_input("nleft/right/backwardsn")
  if response == "left":
        print("You head down the left path. The night is getting darker...")
      elif response == "right":
        print("You head down the right path. The night is getting darker...")
    elif response == "backwards":
        print("You head down the path behind you. You start to see a faint light. As you get closer, you see your family's RV!")
    else: 
        print("Please type one of the following: left/right/backwards.")```

Advertisement

Answer

When creating an if-else statement, all of the if, elif, and else words need to be indented to the same number of spaces.

Your code currently looks like:

  if response == "left":
        print("You head down the left path. The night is getting darker...")
      elif response == "right":
        print("You head down the right path. The night is getting darker...")
    elif response == "backwards":
        print("You head down the path behind you. You start to see a faint light. As you get closer, you see your family's RV!")
    else: 
        print("Please type one of the following: left/right/backwards.")

Notice how the first elif is many spaces in from the if and also the the following else and elif.

Make sure they are all indented the same amount:

  if response == "left":
        print("You head down the left path. The night is getting darker...")
  elif response == "right":
        print("You head down the right path. The night is getting darker...")
  elif response == "backwards":
        print("You head down the path behind you. You start to see a faint light. As you get closer, you see your family's RV!")
  else: 
        print("Please type one of the following: left/right/backwards.")
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement