• Register
0 votes
156 views

Problem :

When I try to compile my Python code below, I get below error

“IndentationError: unindent does not match any outer indentation level”

import sys

def Factorial(n): # Return the factorial

    myresult = 1

    for j in range (1,n):

        myresult = myresult * j

    print "The factorial is: ",myresult

    return myresult

 

Why am I getting above error?

7 5 2
3,870 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

Other posters are probably correct...there are may be spaces mixed in with the tabs. Try doing the search & replace to replace all the tabs with a few spaces.

Try as below :

import sys
def Factorial(n): # Return the factorial
   myresult = 1
    for j in range (1,n):
        myresult = myresult * j
    print "The factorial is: ",myresult
    return myresult
print Factorial(10)

 OR

For the Sublime Text users:

Set the Sublime Text to use tabs for your indentation: View --> Indentation --> Convert Indentation to Tabs

Uncheck a Indent Using Spaces option as well in a same sub-menu above. This will immediately fix your issue.

OR

To easily check for the problems with your tabs/spaces you can actually do as below:

python -m tabnanny yourfile.py

Or you can just set up the editor correctly of course.

I hope the above given solutions will help you in resolving your all of the errors.

9 7 4
38,600 points

Related questions

0 votes
1 answer 66 views
66 views
PROBLEM: Python programming is new to me . I wrote a code which gives me following error: python indentationerror: unindent does not match any outer indentation level The code I wrote is: import sys def Function(x): result = 1 for i in range (1,x): result = result * i * i print ("Your factorial is ",result) return result print (Function(10))
asked Mar 18, 2020 LizzyM 6.1k points
1 vote
1 answer 32 views
32 views
Problem: For my project I wrote this code: def create_train_data(): trainday_data = [1100] for img in tqdm(os.listdir(TRAIN_DIR)): label = label_img(img) path = os.path.join(TRAIN_DIR, img) training_data.append([np.array(img), np.array(label)]) ... np.save('train_data.npy', training_data) ^ IndentationError: unindent does not match any outer indentation level Please help me to fix this poblem.
asked Apr 7, 2020 LizzyM 6.1k points
0 votes
1 answer 15 views
15 views
Problem: When I compile the Python code below, I get IndentationError: unindent does not match any outer indentation level import sys def Factorial(n): # Return factorial result = 1 for i in range (1,n): result = result * i print "factorial is ",result return result Why?
asked Jan 6 Mashhoodch 6.7k points
0 votes
1 answer 102 views
102 views
Problem : When I compile the Python code I get below error IndentationError: unindent does not match any outer indentation level
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
1 answer 2 views
2 views
Problem: I'm trying to create an application in Python 3.2 and I use tabs all the time for indentation, but even the editor changes some of them into spaces and then print out "inconsistent use of tabs and spaces in indentation" when I try to run the program. How can I change ... would be glad if I could get some overall tips on my code, if I have done a lot of mistakes I would be happy to hear.
asked 22 hours ago Mashhoodch 6.7k points
0 votes
1 answer 5 views
5 views
Problem: I'm trying to create an application in Python 3.2 and I use tabs all the time for indentation, but even the editor changes some of them into spaces and then print out "inconsistent use of tabs and spaces in indentation" when I try to run the program
asked Jan 9 Mashhoodch 6.7k points
0 votes
1 answer 30 views
30 views
Problem: I am new to python and I have implemented the list in python and trying to access the list index but it getting me an error that is intend error by for loop don’t know what I am doing wrong in this code. I come from the C background so ... File "main.py", line 46 break ^ IndentationError: unindent does not match any outer indentation level Need someone to help me waiting for help!!
asked Jun 29, 2020 Gavin 15.3k points
0 votes
1 answer 26 views
26 views
Problem: I am a newbee in python programming. I was doing my coding practice. I wrote some codes like : class MyCar: def __init__(self,rang,oil_remaining,**kwargs): self.rang=rang self.fuel_remaining=oil_remaining self.taps = 0 for key,value in kwargs.items(): setattr(self ... and spaces in indentation The code seems right to me but I dont know why am I getting this type of errors. Please help me
asked Mar 22, 2020 LizzyM 6.1k points
1 vote
1 answer 35 views
35 views
Problem: I am newbee in python programming and I have been instructed by my course teacher to do some programming practice. So, I wrote a code: def random(): a = random.randint (0, x+1) b = random.randint (0, y+1) c = random. ... I run this code it gives me following error: RuntimeError: maximum recursion depth exceeded while calling a Python object IndentationError: expected an indented block
asked Apr 6, 2020 LizzyM 6.1k points
0 votes
1 answer 17 views
17 views
Problem: Let me start off by saying that I am COMPLETELY new to programming. I have just recently picked up Python and it has consistently kicked me in the head with one recurring error -- "expected an indented block" Now, I know there are several other ... indentation has not given me better results. I have replaced all of my indents with 4 spaces and even rewritten the code several times.
asked Jan 10 Mashhoodch 6.7k points