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

Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,425 Points

TypeError: 'str' object is not callable TypeError: 'str' object is not callable

Hi,

When I attempt to run percent_letter.py in the Workspaces console it returns this message:

TypeError: 'str' object is not callable

Here is my code:

user_string = input("What is your word?")
user_num = input("What is your number?")

try:
  our_num = int(user_num)
except:
  our_num = float(user_num)

if not '.' in user_num:
  print(user_string(user_num))
else:
  ratio = round(len(user_string)*our_num)
  print(user_string[ratio])

What could be wrong?

Thanks!

4 Answers

akak
akak
29,445 Points

Ah, OK.

You need to change parenthesis to brackets:

if not '.' in user_num:
  print(user_string[user_num])

That way you can grab an index of a letter.

Also, to practice, I would suggest to add another try/except to both cases of conditional. Right now if you input a number that is larger than number of letters in a word you had entered - Python will throw an error. You could avoid that and do a nice message that the number is too high :)

Happy coding ;)

akak
akak
29,445 Points

Do you have the same error or a different one? I used your code and it worked for me as long as you don't enter numbers higher than length of the word. If you enter word "test" you can enter numbers from 0 to 3 and up to 0.8 if we talk about floats. Anything higher than that will cause an error.

akak
akak
29,445 Points

What are you trying to do at this line?

if not '.' in user_num:
  print(user_string(user_num))

If I'd enter word foo, and a number 3 the output of the print would be "foo(3)". Looks like a function and Python tries to treat it as one. But foo is a string. So you get an error that string is not callable - it can't be call like a function since it's just a string :)

Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,425 Points

I'm just following along with what the instructor says in the Python Basics course. From my understanding, what is happening here is that I am supposed to be checking to see if the input of user_num is not a float, then it's supposed to print out the user_string....or something like that. I'm totally confused.

Thanks:-)

Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,425 Points

This is what the console in Workspaces returns when I type the word "magic" and then the number 1:

Traceback (most recent call last): File "percent_letter.py", line 10, in <module> print (user_string[user_num]) TypeError: string indices must be integers


Also, I noticed that there is a file called t(35) in the directory. I have no idea where it came from and when I try to click on it to see what it is there is a message saying "no preview available". I tried to delete it, but Workspaces won't let me.

Maybe it's a bug?

akak
akak
29,445 Points

Ahh, my bad. I did correct one more thing in the code I was testing. Sorry :)

You need to change user_num to our_num in that first print statement.

if not '.' in user_num:
  print(user_string[our_num])