Whenever I try to run this code I keep getting error as shown on the terminal. Please help
Asked
Active
Viewed 242 times
-6
-
Code is not properly intended, https://stackoverflow.com/questions/37143985/visual-studio-code-indentation-for-python/46899704#46899704 – sushanth Jun 21 '20 at 12:11
-
1[Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied and offer poor usability. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Jun 21 '20 at 15:16
2 Answers
0
Even though the code looks properly indented, it is giving IndentationError. My guess is that you could be mixing spaces and tabs. It is unwise to use a mixture of spaces and tabs for the indentation in a single source file.. See also, this other question.
There is no problem with having 2 tabs on each indented line, or having 8 spaces in each.

NotDijkstra
- 199
- 6
-1
You made to much SPACE after your "while"
instead of
while open('myfile.txt',mode='r') as f:
f_con=f.read()
print(f_con)
DO:
while open('myfile.txt',mode='r') as f:
f_con=f.read()
print(f_con)
Python is like that,only press tab once

Jongware
- 22,200
- 8
- 54
- 100

VertexGames
- 97
- 3
-
Okay, Can I use shift+enter to run the code or should use Run from the menu? – Vivek Sharma Jun 21 '20 at 12:19
-
This is incorrect. Python doesn't care how you indent, as long as you are consistent within each block. You can use a single space (though please _don't_: that's insane), or 19 spaces, or seven tabs... you can even mix them if you do it consistently, e.g. a tab followed by two spaces. None of these are good _ideas_, especially in Python, but they all work. Four spaces are generally a good choice as that's what PEP 8 recommends, but it's not mandatory. – ChrisGPT was on strike Jun 21 '20 at 15:15
-
I guess you right python is not my specialty I just remember writing a simple program with python and getting the same error for the reason I mentioned.. – VertexGames Jun 21 '20 at 15:38
-
Configuring your editor to indent in a particular way and letting it indent for you is generally a helpful idea. Your advice to "press tab once" will work in most editors. But there's nothing visibly wrong with the first example. (If one line is indented with a tab and the other with eight spaces, though, it might _look_ correct but _be_ incorrect.) – ChrisGPT was on strike Jun 21 '20 at 15:54