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 trial

Python Python Basics (Retired) Things That Count Exceptions

Cannot get percent_letter.py to work- error text in post

I've gone over this several times, erased my code and retyped it from that in the video, either i'm missing a typo in the same place each time, or it''s just not working for me. Here's my code:

user_string = input("What's your word? ")
user_num = ("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_name)
    print(user_string[ratio])

It asks me for a word, and I input the same as the video, magic. I get the following:

During handling of the above exception, another exception occurred:                                                                         

Traceback (most recent call last):                                                                                                          
  File "percent_letter.py", line 7, in <module>                                                                                             
    our_num = float(user_num)                                                                                                               
ValueError: could not convert string to float: "What's your number? "

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

In your code, user_num, on line 2, is a string because you left out the input function name.

Oh dear gods. How did I miss that?

Thanks!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Haha! It happens to all of us. Get back in there!

KAZUYA NAKAJIMA
KAZUYA NAKAJIMA
8,851 Points

ratio = round(len(user_string)*our_name) should be:

ratio = round(len(user_string)*our_num)?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yeah, it should be. I'm assuming that's just a typo here in the forum instead of in actual code.