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

I don't know what is wrong with this.HELP (PYTHON---TRY AND EXCEPT)

I was just testing my new skills of try and except,but then i came face to face with a TypeError it sayed "TypeError: Can't multiply sequence by non-int by type 'str' " I should get the message "that's not a number" but insted i get a TypeError this means that except block of code is not working the code that I enter is

try:

      a = input('give me a number:  ')

except TypeError:

       print('that's not a number')

else:

       print("hi"*a)

-----------------------------------------HELP!!!--------------------------------------------------------------

I updated my answer with some coda that can help you

1 Answer

input converts every input to a string. It does not matter if you enter a number or a word it gets converted to an string. You can not multiply a string with a string.

I dont see the use of TypeError here it does not prefent the input of words.

Here is what you need:

userInput = input('give me a number:  ')
try:
// try to convert a varaible to an int
   val = int(userInput)
// if it fails throw a exception
except ValueError:
   print("That's not an int!")

Thank you! You are the only one who answers my questions