Mailing List Archive

Invalid literal for int() with base 10?
Ok, I'm not finding any info. on the int() for converting a str to an int (that specifies a base parameter)?! The picture is of the code I've written... And the base 10 paradigm involved?? years = int('y') # store for calculationValueError: invalid literal for int() with base 10: 'y'What is meant by "invalid literal"? I'm trying to convert srt to int, and I didn't know I needed to specify the base. Plus I haven't read anything that I need to specify the base for the int().
Attached is the code, showing the code and the execution of said code.
"When you pass through the waters, I will be with you: and when you pass through the rivers, they will not sweep over you. When you walk through the fire, you will not be burned: the flames will not set you ablaze."     
Isaiah 43:2
--
https://mail.python.org/mailman/listinfo/python-list
Re: Invalid literal for int() with base 10? [ In reply to ]
Ok, I'm not finding any info. on the int() for converting a str to an int (that specifies a base parameter)?! The picture is of the code I've written... And the base 10 paradigm involved?? years = int('y') # store for calculation ValueError: invalid literal for int() with base 10: 'y'
What is meant by "invalid literal"? I'm trying to convert str to int, and I didn't know I needed to specify the base. Plus I haven't read anything that I need to specify the base for the int().
Attached is the code, showing the code and the execution of said code.
Sorry, got pissed and didn't check all the content I sent!


"When you pass through the waters, I will be with you: and when you pass through the rivers, they will not sweep over you. When you walk through the fire, you will not be burned: the flames will not set you ablaze."     
Isaiah 43:2

On Thursday, May 25, 2023 at 05:55:06 PM MDT, Kevin M. Wilson via Python-list <python-list@python.org> wrote:

Ok, I'm not finding any info. on the int() for converting a str to an int (that specifies a base parameter)?! The picture is of the code I've written... And the base 10 paradigm involved?? years = int('y') # store for calculationValueError: invalid literal for int() with base 10: 'y'What is meant by "invalid literal"? I'm trying to convert srt to int, and I didn't know I needed to specify the base. Plus I haven't read anything that I need to specify the base for the int().
Attached is the code, showing the code and the execution of said code.
"When you pass through the waters, I will be with you: and when you pass through the rivers, they will not sweep over you. When you walk through the fire, you will not be burned: the flames will not set you ablaze."     
Isaiah 43:2
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Invalid literal for int() with base 10? [ In reply to ]
On Fri, 26 May 2023 at 10:26, Kevin M. Wilson via Python-list
<python-list@python.org> wrote:
>
> Ok, I'm not finding any info. on the int() for converting a str to an int (that specifies a base parameter)?! The picture is of the code I've written... And the base 10 paradigm involved?? years = int('y') # store for calculation ValueError: invalid literal for int() with base 10: 'y'
>

Imagine giving this to a human. "How many years did you say?" "Oh, y years."

Is that a reasonable way to say a number of years? No. It's an invalid
way of specifying a number of years. Python is a little more technical
in the way it describes it, but the fact is unchanged.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Invalid literal for int() with base 10? [ In reply to ]
On 2023-05-25 22:30, Kevin M. Wilson via Python-list wrote:
> Ok, I'm not finding any info. on the int() for converting a str to an int (that specifies a base parameter)?! The picture is of the code I've written... And the base 10 paradigm involved?? years = int('y') # store for calculationValueError: invalid literal for int() with base 10: 'y'What is meant by "invalid literal"? I'm trying to convert srt to int, and I didn't know I needed to specify the base. Plus I haven't read anything that I need to specify the base for the int().

'12' is a string that contains 2 digits, which together represent the
number 12. 'y' is a string that contains a letter, which doesn't
represent a number.

Perhaps what you meant is that y is a variable that contains a string,
in which case what you want is int(y).

> Attached is the code, showing the code and the execution of said code.

There's no code attached; this list automatically strips attachmentments.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Invalid literal for int() with base 10? [ In reply to ]
"Kevin M. Wilson" <kevinmwilson1956@yahoo.com> writes:
> Ok, I'm not finding any info. on the int() for converting a str to an
> int (that specifies a base parameter)?!

https://docs.python.org/3/library/functions.html#int

> The picture is of the code I've written...

I don't see a picture. The mailing list probably does not accept
attachments. (You don't need a picture anyway.)

> And the base 10 paradigm involved??

The int() constructor takes a base parameter whose default value is 10.
If you specify base=0, it will accept binary, octal, and hexadecimal
numbers in addition to decimal. All this is explained in the link I
gave you.

> years = int('y') # store for calculationValueError: invalid
> literal for int() with base 10: 'y'What is meant by "invalid literal"?

'42' is a valid literal for int(). 'y' is not.

What value did you expect int('y') to give you?

Perhaps you have a variable named 'y' containing a string? If so, you
might want something like int(y) or int(f{'y'}), but int('y') passes the
literal string 'y', which has nothing to do with a variable of that
name.

> I'm trying to convert srt to int,

Do you mean "str to int"?

> and I didn't know I needed to specify the base.

You don't. If you don't specify the base, it defaults to 10.

> Plus I haven't read anything that I need to specify
> the base for the int(). Attached is the code, showing the code and
> the execution of said code.

Any attachment was removed.

> "When you pass through the waters, I will
> be with you: and when you pass through the rivers, they will not sweep
> over you. When you walk through the fire, you will not be burned: the
> flames will not set you ablaze."      Isaiah 43:2

You can add a signature to all your messages if you like, but it will be
very helpful if you introduce it with a line consisting of "-- ", as
I've done here.

It would also be very helpful if you introduce line breaks into your
message, particularly before and after any included code. The
formatting made your message difficult to read.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */
--
https://mail.python.org/mailman/listinfo/python-list
Re: Invalid literal for int() with base 10? [ In reply to ]
On 2023-05-25, Kevin M. Wilson via Python-list <python-list@python.org> wrote:

> Ok, I'm not finding any info. on the int() for converting a str to
> an int (that specifies a base parameter)?!

Where are you looking?

https://docs.python.org/3/library/functions.html#int

> The picture is of the code I've written... And the base 10 paradigm
> involved??

I've no clue what that sentence means.

> years = int('y') # store for calculationValueError:
> invalid literal for int() with base 10: 'y'What is meant by "invalid
> literal"?

It means that the string 'y' isn't an integer literal. The strings
'123' and '-4' are integer literals.

https://docs.python.org/3/reference/expressions.html?highlight=integer%20literal#literals

> I'm trying to convert srt to int, and I didn't know I needed to
> specify the base.

You don't need to unless you want a base other than 10.

> Plus I haven't read anything that I need to specify the base for the int().

Don't know what you mean there.

> Attached is the code, showing the code and the execution of said
> code.

Sorry, I don't see attachments. Include code in posts.

> "When you pass through the waters, I will be with you: and
> when you pass through the rivers, they will not sweep
> over you. When you walk through the fire, you will not be burned:
> the flames will not set you ablaze." Isaiah 43:2

Huh?


--
https://mail.python.org/mailman/listinfo/python-list
Re: Invalid literal for int() with base 10? [ In reply to ]
Op 25/05/2023 om 23:30 schreef Kevin M. Wilson via Python-list:
> Ok, I'm not finding any info. on the int() for converting a str to an int (that specifies a base parameter)?! The picture is of the code I've written... And the base 10 paradigm involved?? years = int('y') # store for calculationValueError: invalid literal for int() with base 10: 'y'What is meant by "invalid literal"? I'm trying to convert srt to int, and I didn't know I needed to specify the base. Plus I haven't read anything that I need to specify the base for the int().
That error message might give the impression that you have to specify
the base, but that's misleading. It's just trying to be helpful, showing
what base was used because the allowed values depend on the base. For
example, these will work:

int('abcd', 16) # abcd is a valid hexadecimal number
int('249', 10)
int('249') # same as above, since base 10 is the default
int('14', 8)

These don't work:

int('abcd', 10) # abcd is not a decimal number
int('abcd') # same as above, since base 10 is the default
int('249', 8) # 249 is not an octal number since 9 is not an octal digit

An error message like "invalid literal for int(): '249'" would be very
confusing because 249 seems to be a valid integer at first sight;
""invalid literal for int(): '249' with base 8" makes clear why it's not
accepted.

--
"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
Re: Invalid literal for int() with base 10? [ In reply to ]
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> "Kevin M. Wilson" <kevinmwilson1956@yahoo.com> writes:
>> Ok, I'm not finding any info. on the int() for converting a str to an
>> int (that specifies a base parameter)?!
>
> https://docs.python.org/3/library/functions.html#int
[...]

Or `print(int.__doc__)` at a Python ">>>" prompt, or `pydoc int`
(or `pydoc3 int`) at a shell prompt. The latter may or may not be
available.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */
--
https://mail.python.org/mailman/listinfo/python-list
Re: Invalid literal for int() with base 10? [ In reply to ]
On 2023-05-26, Grant Edwards <grant.b.edwards@gmail.com> wrote:
> On 2023-05-25, Kevin M. Wilson via Python-list <python-list@python.org> wrote:
>
>> Ok, I'm not finding any info. on the int() for converting a str to
>> an int (that specifies a base parameter)?!
>
> Where are you looking?
>
> https://docs.python.org/3/library/functions.html#int

And don't forget about the help() function:

$ python
Python 3.11.3 (main, May 8 2023, 09:00:58) [GCC 12.2.1 20230428] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> help(int)
Help on class int in module builtins:

class int(object)
| int([x]) -> integer
| int(x, base=10) -> integer
|
| Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is a number, return x.__int__(). For floating point
| numbers, this truncates towards zero.
|
| If x is not a number or if base is given, then x must be a string,
| bytes, or bytearray instance representing an integer literal in the
| given base. The literal can be preceded by '+' or '-' and be surrounded
| by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
| Base 0 means to interpret the base from the string as an integer literal.
| >>> int('0b100', base=0)
| 4
|
| Built-in subclasses:
| bool
|
| Methods defined here:
|
| __abs__(self, /)
| abs(self)
|
| __add__(self, value, /)
| Return self+value.
|
[...]

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