classification
Title: Not convinced with the dynamic data type assignment
Type: compile error Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Sush0907, steven.daprano, terry.reedy
Priority: normal Keywords:

Created on 2020-01-28 12:01 by Sush0907, last changed 2020-01-29 04:44 by Sush0907. This issue is now closed.

Messages (4)
msg360865 - (view) Author: Sushma (Sush0907) Date: 2020-01-28 12:01
Hi 

Please find below example and the compiler error,

when i'm assigning value dynamically and when we comparing in "if" loop it is throwing compiler error. It should not throw error it should assign and act as int why it is thinking as string.
Code Snippet:
print("Hello World")

num = input("Enter number ")

print(num)

if(num%3 == 0):
    num+=num
    print(num)

Output in Console:
Hello World
Enter number 15
15
Traceback (most recent call last):
    File "main.py", line 15, in <module>
       if(num%3 == 0):
TypeError: not all arguments converted during string formatting
msg360870 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-01-28 13:00
The "num" variable is not a number, it *is* a string. Just because you call it "num" doesn't magically turn it into a number. The `input` function returns a string.

You might be thinking of Python 2.7 where `input` automatically evaluated the string as Python code. Python 3 does not do that. If you want to convert the string result of `input` to be a float or an int or some other type, you need to call the `float` or `int` functions.
msg360899 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-01-28 19:33
Sushma, asking about the exception message on python-list was the right thing to do.  You should have waited for the answers you got there.  At this point, bugs in basic python operations are extremely rare.
msg360937 - (view) Author: Sushma (Sush0907) Date: 2020-01-29 04:44
Thank you

On Tue, Jan 28, 2020, 6:30 PM Steven D'Aprano <report@bugs.python.org>
wrote:

>
> Steven D'Aprano <steve+python@pearwood.info> added the comment:
>
> The "num" variable is not a number, it *is* a string. Just because you
> call it "num" doesn't magically turn it into a number. The `input` function
> returns a string.
>
> You might be thinking of Python 2.7 where `input` automatically evaluated
> the string as Python code. Python 3 does not do that. If you want to
> convert the string result of `input` to be a float or an int or some other
> type, you need to call the `float` or `int` functions.
>
> ----------
> nosy: +steven.daprano
> resolution:  -> not a bug
> stage:  -> resolved
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue39476>
> _______________________________________
>
History
Date User Action Args
2020-01-29 04:44:56Sush0907setmessages: + msg360937
2020-01-28 19:33:19terry.reedysetnosy: + terry.reedy
messages: + msg360899
2020-01-28 13:00:44steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg360870

resolution: not a bug
stage: resolved
2020-01-28 12:01:38Sush0907create