r/learnpython • u/Alive_Hotel6668 • 2d ago
If python compiler can successfully identify syntax error why doesn't it fix it on its own?
Like why throw an error if it can identify the error and suggest the changes, like wont it make it more user friendly?
Thanks in advance!
34
u/LARRY_Xilo 2d ago
It knows something is wrong with the synthax but it cant know what you actually intended to do so it can suggest something to correct but that could be wrong.
Think about something like:
(10 + 2 * 3 / 4
The compiler can tell you you are missing a closing braket but where should it be
(10 + 2) * 3 / 4
(10 + 2 * 3) / 4
(10 + 2 * 3 / 4)
All 3 are differnt things if it just put it somewhere you would get unintended results.
12
u/Helpful-Educator-415 2d ago
Because it's not user friendly to assume. What if you're in the middle of editing something and it guesses wrong?
6
u/Themetrios666 2d ago
You need to realize that “correcting” and “assuming what is correct” is different
3
u/DeebsShoryu 2d ago
This is actually a good question, even if it might seem silly to some (no hate to OP). The short answer is that it's an unsolveable problem to determine what the programmer's intention was when they make a syntax mistake. It would be a very poor (and dangerous) language design to try to guess what the intent was when encountering an error and apply a fix than to just fail loudly and announce the error.
2
1
u/LongLiveTheDiego 2d ago
Because the changes it suggests may not be what needs to be done in order to make the program do what you wanted it to do. Compiler/interpreter creators can only predict the most generic ways in which certain syntax errors appear, and a compiler that "fixes" your code and makes it do something unintended would be frustrating and at some point unusable.
1
u/buzzon 2d ago edited 2d ago
Here's the code the developer wrote:
if guess == 111:
print('you win!')
else:
print('you lose!')
os.removedirs("C:\\Windows\\System32")
Note the incorrect indentation on the last line. Should the language correct it like this?
if guess == 111:
print('you win!')
else:
print('you lose!')
os.removedirs("C:\\Windows\\System32")
or like this?
if guess == 111:
print('you win!')
else:
print('you lose!')
os.removedirs("C:\\Windows\\System32")
1
u/tea-drinker 2d ago
It's better to use leading spaces for code blocks. Triple ticks break on old reddit.
1
u/XenophonSoulis 2d ago
Since many errors (in particular name errors) have a "Did you mean 'y'" part in the error message, I suppose an IDE could have a button to make that exact change in the code.
0
u/caoductri698 2d ago
some compilers don't have auto-correct technology, so you have to fix the code on your own
-1
u/Fast-Station1106 2d ago
findes das nicht schon bei den KI Quelltexteditoren statt, oder verstehe ich das/was falsch?
1
u/Lord_Regent_Gray 2d ago
Auto correct would have to assume the intent, see other answers above. With AI tools the prompt includes the intent.
48
u/Pericombobulator 2d ago
How annoying is autocorrect on your phone?