Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
>>> for link in soup.find_all('a'):
... print link
File "<stdin>", line 2
print link
^
IndentationError: expected an indented block
The correct indentation should be:
for link in soup.find_all('a'):
print(link)
Try this snippet to understand the need for indentation
for x in range(3):
print("Inside the loop", x)
print("Outside the loop, this print is run only once")
This notion is well explained in the beginning of Python tutorial:
The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This is the error message I got:
File "main.py", line 15
while True:
^
IndentationError: unindent does not match any outer indentation level
This is my full code:
import wikipedia
from colorama import Fore, Style, Back
y = input("tell me what you want ")
z = int(input("how many sentences "))
try:
text = wikipedia.summary(y, sentences=z)
print('')
print("---Text---")
print(text)
print("----------")
print(len(text.split()),"words.")
except:
print(Fore.RED + "ERROR)
while True:
print("\a")
Can you please explain why this is happening? I am using the Repl online ide.
The answer to this is that I was mixing up tabs and spaces. You shouldn't use both because this error can happen.
While coding in python, it's super important to pay attention to the way you indent. Of course you can either use tab or space, but always make sure you stick with one of them and not mixing them together.
Seems like you haven't done it this time. Delete the indentations and reindent them. Should work fine.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
A problem exists in Python 2.7.11, with the print function:
elif e=="randomize w and x":
random=randint(int(w),int(x))
print random
elif e=="randomize w and y":
random=randint(int(w,int(y))
print random
The boldfaced print shows up as a syntax error, yet all 278 others in my program do not. Why this is, and how I fix it?
The problem is that in
random=randint(int(w,int(y))
a close parenthesis after w is missing, therefore Python thinks the expression continues on next line, but print at that point is a syntax error.
Your problem is not with the print statement, rather the line right before it. The line before hass inbalanced parenthesis:
random=randint(int(w,int(y))
Make sure you balance them out (add an extra ) at the end), and your error on the next line will disappear.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Consider following short Python program:
i= 21;
j= 23;
print (i);
if i>j :
print ('greater')
else :
print 'lesser'
It is giving the error
IndentationError: expected an indented block
What is the cause of this error? (I want to understand it better as I am new to Python.)
You need to indent statements that are intended to be in if-else blocks:
i = 21
j = 23
print (i)
if i > j:
print('greater')
else:
print('lesser')
Any time you have a colon, you follow it with an indented block of text. Everything in that block applies to the thing with the colon (which could be a function that you're defining, an if-statement, for-loop, etc). Indents should be 4 spaces (tabs also work).
Also, you don't need semicolons at the end of each line.
Example:
def function(parameter):
block line 1
block line 2
print function(argument)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
import random
for i in range(1,21):
print("%10d" %(random.randrange(1,7)),
if (i % 5 == 0):
print ("")
What is wrong in this code ?
I know basic python (almost), but i am not able to figure out what could be error in this program.
it is showing this error:
Syntax Error: invalid syntax at line 6 (if statement)
The fourth line is missing bracket .. Thanks all of you
You missed a right bracket )
print("%10d" %(random.randrange(1,7))),
would be correct
You're missing a ) before the last comma on line 4.
Your parenthesis on line 4 don't match. Because you have an unclosed paren, python doesn't report this syntax error until the colon on line 6 (python ignores line breaks as long as you are inside an enclosing set of parenthesis, brackets, or braces).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When i make error?
Don't see errors. Please, help me.
datafile = file('c:\\Users\\username\\Desktop\\test.txt')
for line in datafile:
if '5256' in line:
GLOBACCESS[jid]=100
reply('private', source, u'You license is valid!')
else:
reply('private', source, u'Incorrect password/jid')
Lines 2 and 3 are using spaces while everything else is using tabs. You always need to use the same, and should choose spaces per pep 8
Line 2 shouldn't be indented at all, with spaces or with tabs
for loop shouldn't be indented.
datafile = file('c:\\Users\\username\\Desktop\\test.txt')
for line in datafile:
if '5256' in line:
GLOBACCESS[jid]=100
reply('private', source, u'You license is valid!')
else:
reply('private', source, u'Incorrect password/jid')