Roel,

In order for the code to provide different error messages, it needs a way to 
differentiate between circumstances. 

As far as the int() function is concerned, it sees a string of characters and 
has no clue where they came from. In Python, int(input()) just runs input() 
first and creates a string and then passes it along to int().

You can of course argue there are ways to phrase an error message that may be 
less technicalese.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail....@python.org> On 
Behalf Of Roel Schroeven
Sent: Friday, May 26, 2023 3:55 AM
To: python-list@python.org
Subject: "Invalid literal for int() with base 10": is it really a literal?

Kevin M. Wilson's post "Invalid literal for int() with base 10?" got me 
thinking about the use of the word "literal" in that message. Is it 
correct to use "literal" in that context? It's correct in something like 
this:

 >>> int('invalid')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'invalid'

But something like this generates the same message:

 >>> int(input())
hello
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'hello'

In cases like this there is no literal in sight.

I'm thinking it would be more correct to use the term 'value' here: 
ValueError: invalid value for int() with base 10: 'hello'
Does my reasoning make sense?

-- 
"I love science, and it pains me to think that to so many are terrified
of the subject or feel that choosing science means you cannot also
choose compassion, or the arts, or be awed by nature. Science is not
meant to cure us of mystery, but to reinvent and reinvigorate it."
         -- Robert Sapolsky

-- 
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to