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 trialOleksii Kukharyshyn
470 PointsDoes the program understand everything under 1 as percentage of the word, but everything over 1 as a number of letter?
Case: I've wrote the same code as in video, checked it a few times and tested for different numbers. Problem: some of the float numbers under 1 and every float over 1 return an error. Checked words: "Magic" and "Alex" (short for my name). Problem numbers: 0.9 for "Alex", all float numbers from 1.1 to 2.9 for "Alex", to 3.9 for "Magic". It looks like the round() doesn't work properly, but I'm sure I wrote it just like in the video. Code:
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])
What exactly did I miss?
3 Answers
Stephen Link
3,685 PointsYou aren't getting errors because round() isn't working but rather because of how it is being used. In this program the idea is that you enter some float less than 1 to get a percentage of the word's length. If you enter a number greater than 1, and then multiply this by the length of the string, you will most likely wind up with a number which is larger than the length of the string. If a string only has 5 characters you can't access a character at any position higher than 4 [strings start addressing at 0] because the character just doesn't exist.
0.9 is giving you problems on "Alex" because the length of Alex is 4, and 4 * 0.9 = 3.6, and this is rounded up to 4. Since we start at index 0 there is no location 4 in "Alex" and therefore you get an error when you ask for the letter in that location.
Kenneth Love
Treehouse Guest TeacherYeah, it's not a perfect program :) It's more for illustrating how to work with numbers and strings at the same time than it is how to build an amazing piece of software. Ideally, you'd make sure that no number greater than 1 came in for a float (or that you looped around the word however many times needed) and that any integer you ended up with would be lower than the len()
of the word.
All of that, though, is way too much to go over or expect students to do in such a basic course.
Oleksii Kukharyshyn
470 PointsThanks a lot! It is still a bit confusing for me, but I now understand it, more or less. I thought of limiting the possible answers to second question, but doing this from a technical side (not just by asking user to write a number from 0 to (len(user_string)-1)), but I guess, it would really be an overcomplication at this point.