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
akshat mehrotra
2,727 PointsI 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!!!--------------------------------------------------------------
1 Answer
tobiaskrause
9,160 Pointsinput 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!")
akshat mehrotra
2,727 PointsThank you! You are the only one who answers my questions
tobiaskrause
9,160 Pointstobiaskrause
9,160 PointsI updated my answer with some coda that can help you