0

Type Conversion

Hello so quick question regarding this problem for python: What is the output of this code? >>> float("210" * int(input("Enter a number:" ))) Enter a number: 2 The answer is 210210.0 I'm confused because I would have guessed 420.0 had I not entered it. Reason behind it is because I thought when you add "int" to a string it changes it to literally adding the string together to a math problem Thank you in advance all advice is welcome and appreciated

30th Dec 2016, 4:16 AM
justin
3 Respuestas
+ 2
yes the int conversion does occur on the input string but with your code the input multiplication on a str is occuring before the float conversion. alter the order of operations float("210") * int(input("Enter a number:" ))
30th Dec 2016, 4:29 AM
richard
+ 2
Well, if you hardcode the "210" value, so you don't need to type it in between quotes, then 210 will be an int, while "210" will be a string. In the same way, 210.0 will be a float, while 210 will be a int... >>> 210.0 * int(input("Enter a number")) But, I guess maybe you want the input being also float, since you'll convert the entire result in float ^^ In this case, you should then write: >>> 210.0 * float(input("Enter a number")) Or if you only want intergers calculs: >>> 210 * int(input("Enter a number"))
30th Dec 2016, 7:30 AM
visph
visph - avatar
0
Okay thank you! Just have to bring it back to basic algebra.
30th Dec 2016, 4:50 AM
justin