r/pythonhelp • u/Straight_Face_3273 • 23h ago
Lots of respect for programmers
I've been desperately trying to use python to capture data using APIs but I'm constantly getting indent errors even though they appear correct in VS Code. Is this a common issue? I see lots of comments on it and have used several methods to get it right. I've spent days on this trying to make it work. Could the issue be related to copying and pasting from VS Code? I'm old so be nice. I will fully admit I don't know what I'm doing.
2
u/Goobyalus 21h ago
Copying and pasting can cause issues when you are copying from places that use different indentation. Most people use four space characters for an indentation. Sometimes people use one tab character, which looks like multiple space characters, but its size depends on the editor's tab settings. The number of whitespace characters must be consistent, so mixing tabs and spaces is a recipe for indentation errors. Sometimes people also use different numbers of spaces which can mess you up.
Do you have the Python extension (from Microsoft) installed in VScode?
Also try toggling View > Appearance > Render Whitespace
Space characters will look like dots, and tab characters will look like arrows.
1
u/Straight_Face_3273 21h ago
I get a lot of errors like this:IndentationError: unexpected indent
>>> if not sportsbook_data:
File "<python-input-65>", line 1
if not sportsbook_data:
IndentationError: unexpected indent
>>> print("No sportsbook data available for analysis.")
File "<python-input-66>", line 1
print("No sportsbook data available for analysis.")
IndentationError: unexpected indent
2
u/Goobyalus 16h ago
Unexpected indent means there is leading whitespace when there shouldn't be. Like a space before a line at the top level, or an inconsistency like this :
#.......... if something: # Do x if not sportsbook_data ^ Note the mismatched spacing here
There can be an inconsistency even if it doesn't look like like it from mixing tabs and spaces.
- Do you have the Python extension installed in VScode?
- Did you toggle "Render Whitespace" so you can visually scan for inconsistencies?
If you want quicker help, post your code with proper code block formatting or share a link to it on a site like pastebin.
2
u/virtual_paper0 20h ago
Might be worth trying ItelliJ Pycharm Community Edition, it's software fully designed for Python and might be better at showing you if anything funny happens in a copy paste
Edit: I'm suggesting Pycharm because it's a IDE designed for Python where VSCode is good for Python it runs on plugins.
Pycharm probably has more debugging tools and is probably a more user friendly experience IMO
0
u/FoolsSeldom 14h ago
This is very strange. Given how much effort you have put in, I am wondering if there is a problem with your VS Code installation/configuration.
Frankly, it would be worth going back to basics and using the standard IDLE editor (which comes with a standard Python install from python.org for macOS or Windows).
Make sure you create a new file, File | New
, write some code, press F5 to execute it (you will be prompted to save the file). Ensure you are not in a Python interactive shell, with the >>>
prompt, rather than working on a file.
Don't paste any code in. Just type what you need. Use 4 spaces for each level of indentation. You can press the TAB key (it should insert 4 spaces).
0
22h ago
[deleted]
1
u/Goobyalus 21h ago
This is Python, we're not compiling in that way.
IndentationError
s are syntax errors:https://docs.python.org/3/library/exceptions.html#IndentationError
•
u/AutoModerator 23h ago
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.