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 trialPaul Ngwendere
9,138 PointsRaising exceptionns
i tried running this code and it did but it keeps on returning "good idea" even though there are less than 3 characters..where did i go wrong?
def suggest(product_idea):
if len(product_idea) < 3:
raise ValueError("More characters needed.")
return product_idea + "inator"
try:
users_idea = str(input("What's your idea? "))
except ValueError as err:
print("Try again..")
print("({})".format(err))
else:
print("good idea!")
1 Answer
Donald Morales
15,983 PointsHello Paul!
The reason the code defaults to the else statement block is because your suggest() function isn't in the try block:
def suggest(product_idea):
if len(product_idea) < 3:
raise ValueError("More characters needed.")
else:
return product_idea + "inator"
try:
users_idea = input("What's your idea? ")
except ValueError as err:
print("Try again..")
print("({})".format(err))
else:
print("good idea!")
Placing the the function there will get the code running as expected:
try:
users_idea = input("What's your idea? ")
suggest(users_idea)
Paul Ngwendere
9,138 PointsPaul Ngwendere
9,138 PointsThank you Donald..It's now working..