git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[docs] Dynamic Data type assignment


On 29/01/20 4:51 PM, sushma ms wrote:
> Thanks a lot.
> 
> But why can't we make output of input also dynamic data assignment.

1 please don't 'top post': the normal sequence of a conversation is 
question THEN answer!

2 ambiguity! It is not possible for the input() mechanism to tell 
whether you later intend to use the number as int(15) or as "15", ie 
str(15).

Thinking about it, what if the user entered "abc" (there's nothing to 
stop him/her!); how could that translate to a number? Hence, all input 
(one assumes, from the keyboard) is taken to be a string. If you want to 
use it otherwise, then programmatically convert the string into a number 
(and take care of any errors which may arise).

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


> Thanks & Regards,
> Sushma MS
> 
> On Tue, Jan 28, 2020, 9:54 PM Mariatta <mariatta at python.org> wrote:
> 
>> input() returns a string. If you want it to be treated as an int you need
>> to cast it, example:
>>
>> num =int(input ("Enter number"))
>>
>> On Tue, Jan 28, 2020, 5:13 AM sushma ms <sush0907 at gmail.com> wrote:
>>
>>> 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
>>> Created the issue:
>>> msg 360865 created
>>> issue 39476 created
>>> --
>>> Thanks & Regards,
>>> ---------------------------------
>>> Sushma
>>> Mob:9740055884
>>> _______________________________________________
>>> docs mailing list -- docs at python.org
>>> To unsubscribe send an email to docs-leave at python.org
>>> https://mail.python.org/mailman3/lists/docs.python.org/
>>>
>>

-- 
Regards =dn