+ 12

Why isn't the float being converted to an integer?

I started learning python recently and I am making a code with all the things I am learning about python so I can assimilate them better. However, in the last line of my code I tried to convert a float into an integer with the function "int()" but it doesn't work. How can I convert that float to an integer? Here is my code: https://code.sololearn.com/cd0eBeOvroLP/#py

24th Mar 2019, 12:08 AM
Afonso Hipólito
Afonso Hipólito - avatar
7 ответов
+ 13
I think the function is working fine but because n is a string you are getting an error. Change "n" to "int(n)" in that print and it should work fine. EDIT: No Problem 👍
24th Mar 2019, 12:36 AM
LynTon
LynTon - avatar
+ 9
you need also to make int(n)
24th Mar 2019, 12:33 AM
Hubert Dudek
Hubert Dudek - avatar
+ 8
Also, the last line produces a float because the result of division is always a float, even if the integers divide evenly. E.g. print(6 / 3) outputs 2.0 print(int(6 / 3)) outputs 2
24th Mar 2019, 2:44 AM
David Ashton
David Ashton - avatar
+ 6
Thanks Hubert.
24th Mar 2019, 1:35 AM
Afonso Hipólito
Afonso Hipólito - avatar
+ 6
Thanks LynTon, I'll try that
24th Mar 2019, 1:36 AM
Afonso Hipólito
Afonso Hipólito - avatar
+ 2
Change 'n' to 'int(n)' to pass it as an integer. Should work
24th Mar 2019, 12:41 PM
Yugyn Dprodigy
Yugyn Dprodigy - avatar
0
Change n to int
24th Mar 2019, 6:23 PM
George S Mulbah II
George S Mulbah II - avatar