Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDaffa Alif Pratama
2,316 PointsCannot convert to integers
Earlier in the video, Kenneth said that we can convert float to integer by using the int() function and put the floats into it e.g: int(2.54) will be converted to 2
But, when I enter decimal number when input_int being asked, it shows this error:
Traceback (most recent call last):
File "string_multiply.py", line 4, in <module>
print(input_string*int(input_int))
ValueError: invalid literal for int() with base 10: '2.54'
Does anyone has any explanation about this? Thank you! :)
2 Answers
Kevin Roundtree
4,575 PointsTry using int(floor(input_int)).
Jesse Pavlis
18,088 PointsThe issue here is that the input_int
variable is a string, not a float. Try int(float(input_int))
Daffa Alif Pratama
2,316 PointsThis error shows up:
What's ya number? 1.25
Traceback (most recent call last):
File "percent_letter.py", line 13, in <module>
print(user_string[ratio])
IndexError: string index out of range
Daffa Alif Pratama
2,316 PointsDaffa Alif Pratama
2,316 PointsThanks for the answer! But unfortunately, it gives me this error:
File "string_multiply.py", line 4, in <module> print(input_string*int(floor(input_int))) NameError: name 'floor' is not defined
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 Pointsfloor
is in themath
module. Need to add:from math import floor