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 Functions and Looping Raise an Exception

Sybil Grey
Sybil Grey
690 Points

EOF error?

I'm not sure why my code is throwing an EOF error. When I paste it into workspace, it functions just fine.

suggestinator.py
#For the sake of the question, it gives me the following error twice.
#ERROR: test_exception_not_raised (__main__.TestRaiseExecution)
#----------------------------------------------------------------------
#Traceback (most recent call last):
#  File "", line 23, in test_exception_not_raised
#  File "/workdir/utils/challenge.py", line 24, in execute_source
#    exec(src)
#  File "", line 1, in 
#EOFError: EOF when reading a line

product_idea = input("What is your product idea's name suggestion?  ")
number_of_characters = len(product_idea)

def suggest(product_idea):
    if number_of_characters < 3:
        raise ValueError("Your idea must be 3 or more characters long.")
    return product_idea + "inator"

try:
    result = suggest(product_idea)
except ValueError as err:
    print("Oh no! Your idea must be a minimum of 3 characters. Please try again!")
else:
    print("Congratulations! Your new product name is: " + "{}".format(result))

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey Sybil Grey 👋

You'll want to avoid writing code outside of the function. The tests for this challenge are setup to call the suggest function with different values and check if the outcome is the expected value (a string returned or a ValueError raised).

The tests are not expecting any user input or additional suggest calls, so instead of adding logic globally you'll only want to raise the error inside the function. The tests will handle the rest 🙂

def suggest(product_idea):
    if len(product_idea) < 3: 
        raise ValueError("Must be 3 characters or more")
    return product_idea + "inator"

Hope this helps!