Although there is certainly room good improvement in the error messages,
making error messages that work in all cases is hard.

For example, functions are just another kind of variable, and it is
possible for arbitrary variables to act like functions. So error messages
that try to differentiate between the two can't with reliably.

Also, for list index errors, the error is created by the list class, and it
doesn't know the variable name, and there may not even be a variable.  Can
you give an error message that will work in your example and also work in
this example:

sorted(x%i for i, x in enumerate(range(3:999)))[::5].__getitem__(1000)

This example seems very different to us but is identical as far as the list
class is concerned, so you would need a single error message that works
equally well in both cases.

Syntax errors are also often hard.  Consider your example, but with more
lines around it:

a = [step %s' % i
for i in l
]

Now your error message will give the wrong information.  It often is very
hard to even tell what line a syntax error occurs on, not to mention figure
out exactly what went wrong.

So yes, better error messages would be great.  But the sorts of error
messages you describe are not generally possible in practice.  There are
just too many corner-

On Nov 28, 2016 11:58 PM, "victor rajewski" <askvic...@gmail.com> wrote:

> I teach a computing subject to high school students using Python as our
> primary language. One thing that often causes confusion at the start is
> error messages/exceptions. I think it would lower entry point quite a bit
> by making error messages more readable to novices. Recent research found
> reduced frequency of overall errors, errors per student, and repeated
> errors when error messages were enhanced [1].
>
> This could be implemented by a command-line switch when running python, or
> a different python executable (along the lines of pythonw.exe vs
> python.exe) such as python-easy. A simpler error message might get rid of
> excess information which can confuse the novice (such info might be dumped
> to a file to allow more advanced users to debug), provide a brief
> description of what the error might mean, and/or how you might fix it. So a
> SyntaxError might tell the user that they've probably forgotten a :, and a
> NameError might say that the item has been defined yet, or they've made a
> typo. A couple of examples follow:
>
> Traceback (most recent call last):
>
>  File "foo.py", line 2, in <module>
>
>    l[10]=14
>
> IndexError: list assignment index out of range
>
> A better message might be:
>
> You tried to use l[10] when l is only 4 elements long. You can add items
> to l using l.append(value), or check your index value to make sure that's
> really the position you wanted to access.
>
> Traceback (most recent call last):
>
>  File "foo.py", line 2, in <module>
>
>    while name != "quit" and reponse != "quit":
>
> NameError: name 'reponse' is not defined
>
> A better message might be:
>
> You're trying to use the value of 'reponse', but that variable hasn't got
> a value yet. You can give it a value earlier in the code, or it could be a
> typo. You have a variable called 'response' - is that the one you meant?
>
> Traceback (most recent call last):
>
>  File "foo.py", line 2, in <module>
>
>    print(length(l))
>
> NameError: name 'length' is not defined
>
> A better message might be:
>
> Python doesn't recognise the function "length". Did you mean len?'
>
>  File "foo.py", line 2
>
>    for i in l
>
>             ^
>
> SyntaxError: invalid syntax
>
> A better message might be:
>
> You have a for loop without a : (colon). Try adding a colon at the end of
> the line.
>
>
> Any thoughts?
>
> [1]: http://researchrepository.ucd.ie/handle/10197/7583
> --
>
> Victor Rajewski
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas@python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to