This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author TheDoctor165
Recipients TheDoctor165, docs@python, paul.moore, steve.dower, terry.reedy, tim.golden, zach.ware
Date 2021-08-08.17:49:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1628444991.92.0.991529189283.issue44866@roundup.psfhosted.org>
In-reply-to
Content
I first noticed this and reported it on the W3 Schools Tutorial, the section entitled "Add Two Numbers with User Input"

There were many behaviors that I did not understand, but for this bug report, I will state that the input statements present seem to return a string and under most situations will return an error if the user inputs a real number like 2.8.  However, under a very specific situation, it will truncate 2.8 to 2 without error.  

After further investigation, I believe the following session in the IDLE's output window  and editor illustrates this inconsistent behavior.  Note that I have added comments after copying the session here...

>>> print(x)  #want to ensure new session has x as undefined
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    print(x)
NameError: name 'x' is not defined  # confirmed x is undefined
>>> x="2" # define x as the string "2"
>>> print(x)
2
>>> print(type(x)) # confirm that x is a string value of "2" 
<class 'str'>
>>> y=int(x) # convert string value of "2" to integer of 2  -
# according to documentation this should work - see "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."
# at link --> https://docs.python.org/3.9/library/functions.html#int
>>> print(type(y)) # ensure y is type int
<class 'int'>
>>> print(y)
2
>>> z=x+".8" # create z to be the concatenation of two strings "2" and ".8" = "2.8", a string representation of the real number 2.8
>>> print(z)
2.8
>>> print(type(z)) # ensure z is a string
<class 'str'>
>>> aa=int(z) # convert z to an integer (as descried in the link
# above, this should NOT work
Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    aa=int(z)
ValueError: invalid literal for int() with base 10: '2.8'
>>> w="2.8" # Define w directly as the string value of 2.8 = "2.8"
>>> bb=int(w) # This should also produce an error
Traceback (most recent call last):
  File "<pyshell#36>", line 1, in <module>
    bb=int(w)
ValueError: invalid literal for int() with base 10: '2.8'
>>> a='2.8'
>>> b=int(a)
Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    b=int(a)
ValueError: invalid literal for int() with base 10: '2.8'
>>> print(type(a)) # Ensure a is a string
<class 'str'>
>>> w="2"
>>> bb=int(w)
>>> print(bb)
2

>>> print(type(bb))
<class 'int'>
>>> test=int(input("What is test value? ")) #lets try inputting a
# real number but as an argument to int and assigning it to test
What is test value? 2.8 # this should not work either
Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    test=int(input("What is test value? "))
ValueError: invalid literal for int() with base 10: '2.8'
>>> # So everything here is working as expected, but...

Here is code from the IDLE editor... a file called testinput1.py

x = int(1)
y = input("Type a number: ")
print(type(y))
int_y = int(2.8) #conver y to an integer 2 and assign to int_y
z = int("3")
print(x)
print(y)
print(int_y)
print(z)

# I can find no documentation to suggest this should work, but it does.  Here is the output in IDLE's shell

Type a number: 2.8
<class 'str'>
1
2.8
2
3

Now, if I immediately go into the shell while the variables remain untouched and defined...

>>> a=int(y) # Correctly, this produces the expected error
Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    a=int(y)
ValueError: invalid literal for int() with base 10: '2.8'

After extensive testing, I conclude that after input, you may immediately apply the int() function to the resulting string, but you quickly lose that ability, resulting in the expected error.  I can find no documentation to explain this behavior.  

If I am not overlooking something, I think this should either be in the documentation of the function int(), if it is intended to behaviour this way, or as a bug, should be corrected.

NOTE, I just started learning Pytyon this weekend, so I may be just ignorant of the behavior, but I have searched a good bit and found nothing suggesting this is how int() should behalf.  I have also not studied the other constructor functions.
History
Date User Action Args
2021-08-08 17:49:51TheDoctor165setrecipients: + TheDoctor165, terry.reedy, paul.moore, tim.golden, docs@python, zach.ware, steve.dower
2021-08-08 17:49:51TheDoctor165setmessageid: <1628444991.92.0.991529189283.issue44866@roundup.psfhosted.org>
2021-08-08 17:49:51TheDoctor165linkissue44866 messages
2021-08-08 17:49:51TheDoctor165create