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 trialsreeharsha thanneeru
Courses Plus Student 1,297 PointsBug in the code in example
If you notice, as the index starts with a 0, when we use percentage, it actually has one number more than actual index value.
For e.g. if I give the user_num as 0.8 (80%) for a 5 letter world (e.g. 'house'), we notice that ideally the output should have been 4th letter 's', but in case as it refers to index, the output number would be 'e'.
So I think so we need to modify the code,to add a '-1' and put it as below.
if not '.' in user_num: print(user_string[our_num-1]) else: ratio = round(our_num*len(user_string)) print(len(user_string)) print(user_string[ratio-1])
1 Answer
Will Beasley
6,020 PointsTrue it is a bug but not for that reason. The bug presents itself when you go outside the bounds of the list. This happens when you hit .91<=. Also, while you are on the right track yours would throw an error if a user enters .1 >. To fix it you would really need a nested if statement to the effect of
if(ratio < .1): ratio = round(our_num*len(user_string)) print(len(user_string)) print(user_string[ratio]) else: ratio = round(our_num*len(user_string)) print(len(user_string)) print(user_string[ratio-1])
Additionally, you wouldn't need to add a -1 for when a user enters an int. For two reasons. First, it is directly accessing an element in the list so that isn't a problem. Second, for the reason listed above. If the user enters '0' that would take them outside the bounds of the list. Hope that helped!