Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (Retired) Things That Count Exceptions

Yasin Baala
Yasin Baala
253 Points

Variable "ratio" messes everything up

So I wrote a similar code to the one Kenneth had in the video, but forgot to put in the variable named "ratio" to the script to represent round(len(input_string)*our_num). Actually, I didn't use the len() function at all.

input_word = input("What's your word? ")
input_number = input("What's your number? ")
try:
  our_num = int(input_number)
except:
  our_num = float(input_number)
if '.' not in input_number:
  print(input_word[our_num])
else:
  print(input_word[(round(our_num))])

Still works like a charm. I named this file "percent_letter.py" I then made another file then using a variable named "ratio" to represent round(len(input_string)*our_num) and titled it "percent_letter_2.py" which looks like this:

input_word = input("What's your word? ")
input_number = input("What's your number? ")
try:
  our_num = int(input_number)
except:
  our_num = float(input_number)
if '.' not in input_number:
  print(input_word[our_num])
else:
  ratio = round(len(input_word)*our_num)
  print(input_word[ratio])

This script gives me an "IndexError: string index out of range" for the last line of code every time I input a decimal in response to the second prompt of "percent_letter_2.py". So my question is, why doesn't "percent_letter_2.py" run even though the only difference is that I used a variable and the len() function in this case?

Yasin Baala
Yasin Baala
253 Points

Actually, if someone could tell me a better way to copy/paste my work to be viewed as a script in the discussions, that'd be great. :c

Pasting the code in would be really helpful. It is pretty hard for me to follow what you are saying with out seeing it.

The markup for inserting python is as follows. First type three `followed by the word python with no spaces. Hit enter then paste in your code. At the end of your code hit enter once to start a new line then type" three more times.

example:

this_is = "some code"
that = "I"`
def initely():
    wrote = ""

Don't forget to have blank lines before the first three ` and after the last three or your code won't actually show up as a code block.

I'm following this question so I'll answer as soon as I can understand it better. : )

Edit: I realized that the "`" character simply bolded my text which was not helpful. I put the " around the character in question so that it shows up correctly.

1 Answer

Matthew Rigdon
Matthew Rigdon
8,223 Points

Well, an IndexError means that you are trying to call a certain string position that doesn't exist. For example:

name = "Matt"
print(name[4])

This will give you an IndexError, since there is no indexed value of 4 within "Matt." Also, the two codes you posted do not do the same thing. Here is an example why:

input_word = "Matt"
our_num = .4

ratio = round(len(input_word)*our_num) #round(len("Matt")*.4) gives you round(4*.4) - round(1.6) - 2.
print(input_word[ratio]) #input_word[2] is 't'

First code:

input_word = "Matt"
our_num = .4

print(input_word[(round(our_num))]) #Gives you - input_word[round(.4)] -> input_word[0] -> prints 'M'
Yasin Baala
Yasin Baala
253 Points

Thanks, Matt, now I see why it worked for Kenneth in the video. The only difference between my code and his code is that mine can't accurately index strings when a percentage is input. So the problem was never really with the scripts, it's just that my index error was a result of a large input.

Also, thank you David, now I now know how to paste code into a block so it can be read. Couldn't have gotten a response without you! : )