0

My code was :- float(210*input("enter a number")) take the number as 2 ,can someone tell me y that output..?

Basic question

15th Nov 2016, 11:15 AM
Scoobydoo
11 ответов
+ 3
The input is a string so %210*input("enter a number")% evaluates to %210*"2"%, i.e. a string of 210 "2" char. Converted to a float the resut is: 2.2222222222222224e+209 (on my machine). If you expected 420.0 your code should be %print(210*float(input("enter a number")))%
15th Nov 2016, 11:58 AM
Jean-Hugues de Raigniac
Jean-Hugues de Raigniac - avatar
+ 3
convert directly to float the input: %float(input("enter a number"))%
15th Nov 2016, 12:01 PM
Jean-Hugues de Raigniac
Jean-Hugues de Raigniac - avatar
+ 3
You already have your answer so this is just clarification. The NUMBER 2 is stored in memory as binary: 0000 0010 (2) The STRING '2' is stored in memory as binary: 0011 1000 (50) Because the symbol '2' is at ASCII position 50. Strings take more space to represent than numbers, because the symbols are recorded, not the number you see. Taking integer 50 as an additional example: 50 = 0011 1000 (8 bits) '50' = ASCII '5' (53) followed by ASCII '0' (48) (total 16 bits)
15th Nov 2016, 2:58 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
In this case 420.0. In the exercise there's float("210"*input("Enter the number")) and the input was 2. If you consider 210 as a string (this for "...") you repeat the string twice. With the function "float" you obtained 210210.0
15th Nov 2016, 11:39 AM
Leo
Leo - avatar
+ 1
Perphaps for the % that you put before and after the code. I'm not an expert.
15th Nov 2016, 11:47 AM
Leo
Leo - avatar
+ 1
Happy to help. If an answer satisfy you it is a good habit to upvote it.
15th Nov 2016, 12:18 PM
Jean-Hugues de Raigniac
Jean-Hugues de Raigniac - avatar
+ 1
ans will be 210210.0, since 210 is an integer value but by default input taken from user is in string format, in this case you are not converting the input string, so ans seems to be.. 210 * "2" = 210210 int * str = str so final ans will be 210210.0, as you are converting it to float
14th Dec 2016, 11:13 PM
Rahul Khanna
Rahul Khanna - avatar
0
but for the code that I asked ,idle shows output as 2.2222222222222222224e+209
15th Nov 2016, 11:43 AM
Scoobydoo
0
No no I didn't put that in the code that's just to tell u guys the code ,it's like comment ,The code I wrote was without those % ,well anyways thanks for ur time
15th Nov 2016, 11:50 AM
Scoobydoo
0
@jean Yeah that's the answer that I got on my PC too ,but the input is 2 so isn't that a number rather a string ,so shouldn't the answer be float(210*2)??
15th Nov 2016, 11:57 AM
Scoobydoo
0
ohhh yeah I get it now thanks Jean !
15th Nov 2016, 12:07 PM
Scoobydoo