Python Python Basics Functions and Looping Raise an Exception

Cedrick Cyizere
Cedrick Cyizere
275 Points

I am getting an exception not raised error and an EOF error i don't know what seems to be the problem .

suggestinator.py
def suggest(product_idea):
    if len(product_idea) <= 3:
            raise ValueError("the value is less than 3 characters")
    return product_idea + "inator"
try: 
     suggest( product_idea=(input("please choose a product ")))
except ValueError as err:
    print("the value is less than 3 characters")
else : 
     print (product_idea)

2 Answers

Joseph Yhu
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Joseph Yhu
PHP Development Techdegree Graduate 46,902 Points

You don't need any of that extra stuff; all you need is this part:

if len(product_idea) <= 3:
    raise ValueError("the value is less than 3 characters")

Actually, the extra stuff might be why you are getting errors.

Luke Davis
Luke Davis
274 Points

I've seen the answer now just want to know why this doesn't work i can run it and it does what the task asks and runs fine just keep getting Traceback (most recent call last): File "", line 30, in test_exception_raised File "/workdir/utils/challenge.py", line 24, in execute_source exec(src) File "", line 6, in ValueError: the product has to be longer than 3 characters

Any chance you can elaborate

def suggest(product_idea): return product_idea + "inator" product_idea = "a"

if len(product_idea) < 3: raise ValueError("the product has to be longer than 3 characters")

else: print(product_idea)

Joseph Yhu
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Joseph Yhu
PHP Development Techdegree Graduate 46,902 Points

Again, you don't need the else clause; you only need:

if len(product_idea) < 3:
    raise ValueError("the product has to be longer than 3 characters")

And you should put it before the return.