New submission from John Joseph Morelli <johnjmore...@hotmail.com>:
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. ---------- assignee: docs@python components: Build, Documentation, IDLE, Library (Lib), Windows files: function_int_08Aug21.txt messages: 399224 nosy: TheDoctor165, docs@python, paul.moore, steve.dower, terry.reedy, tim.golden, zach.ware priority: normal severity: normal status: open title: Inconsistent Behavior of int() type: behavior versions: Python 3.9 Added file: https://bugs.python.org/file50205/function_int_08Aug21.txt _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue44866> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com