I have a question about the int() function of Python. If you put 1.00 in int(input()) it makes an error different from int(1.00), why is that?

Asked 2 years ago, Updated 2 years ago, 32 views

I executed it as follows.

c = int(input())
1.00
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    c = int(input())
ValueError: invalid literal for int() with base 10: '1.00'

b = int(1.00)
print(b)
1

I think both c and b should have 1 but it's different. How can I correct the int function's own error?

For your information, the original plan is

a = list(map(int, input().split()))

Like this.

1.00 -6.00 11.00 -6.00

It was to get input at once.

python python3

2022-09-22 19:29

1 Answers

Looking at the comments, I think you've already found an answer. However, for those who come to this post later, I will explain exactly why the error occurred.

Int behaves differently depending on the type of factor.

The int function is a hidden function of x, x.Returns __int__().

x must be an integer consisting of only 0-9 (radix base). If something other than radix-base is included, the int function fails to handle it and spouts an error.

For more information,

more relevant documents refer to the it appears to be.

Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, return x.__int__(). If x defines x.__trunc__() but not x.__int__(), then return if x.__trunc__(). 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 radix base.


2022-09-22 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.