简体   繁体   中英

IndentationError - expected an indented block

I get the IndentationError: expected an indented block. I was trying to copy all the contents form webpage to a txt file. Not sure what the error is but in my homework found that indented error occure if there is a combination of space and tabs. Beginning with python can someone help. Thanks in Advance.

import requests
url = 'https://seekingalpha.com/article/4166013-t-t-q1-2018-results-earnings-call-transcript?part=single'
data = requests.get(url)
with open('file.txt','w') as out_f:
out_f.write(data.text.encode('utf-8'))

with expects an indented block is to follow, so do this:

with open('file.txt','w') as out_f:
    out_f.write(data.text.encode('utf-8'))

This is the same as when you indent for an if , elif , else , for , while , try , etc.

Unlike many other languages Python uses indentation to create code blocks. Python code blocks are usually indented with 4 or 8 spaces. You should pay attention to the indentation of the code you find online when copy/pasting. All that said, copy/pasting random code off the Internet is a good way to get a virus or a 0 on a lab assignment.

Here is some more details about Python indentation: https://www.python-course.eu/python3_blocks.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM