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 trialKate Petry
Courses Plus Student 9,432 PointsI'm getting an error after I enter my number that indicates string index is out of range
I have reviewed my code and it looks like what Kenneth has. When I run the code it asked for my word and my number. After I enter the number I get an error that says string index is out of range. I am having trouble understanding why. Here is the code I have.
user_string = input("What's your word? ")
user_num = input("What's your number? ")
try:
our_num = int(user_num)
except:
our_num = float(user_num)
if not '.' in user_num:
print(user_string[our_num])
else:
ratio = round(len(user_string)*our_num)
print(user_string[ratio])
3 Answers
Kenneth Love
Treehouse Guest TeacherWhat string and number are you entering?
Kate Petry
Courses Plus Student 9,432 PointsI tried my name as the string, kate, and for the number I tried 44
Kenneth Love
Treehouse Guest Teacher"kate" doesn't have 44 characters in it, though, so doing "kate"[44]
will give the error that you described (index is out of range). Try "kate" and 0, 1, 2, or 3.
Kate Petry
Courses Plus Student 9,432 PointsThank you. I misunderstood the concept of the video. Rewatching again.