Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Why is + "inator" not working for me?

Hello!

Firstly, I feel VERY proud of this code. It's taken me hours to get it to work lol. Secondly, I'm not sure what it is but, everything works except the + "inator" part in the first code below. I started looking in the community and I saw, when compared to others (thanks sadique naikwadi), that in my final else, I was printing the product_idea variable.

When I created another variable called ideas, and I assigned the whole suggest(product_idea) value, it finally added the "inator" part at the end of the valid name entered. Can someone please tell me what did I miss? I guess I'm not sure why I'd have to create a new variable and assign it the whole suggest(product_name) value for the "inator" part to be added, instead of just calling product_idea by it's own.

Also, when I try to run/check work my working code on the challenge window, it says that it contains errors and it fails. When I run it in Visual Studio Code, it works just fine.

Thanks in advance!

def suggest(product_idea):
    return product_idea + "inator"#I'm unable to get this to work.

try:
    product_idea = input("Please, enter a name: ")
    if len(product_idea) <= 3:
        raise ValueError("Names need to be more than 3 characters long.")
except ValueError as err:
    print("That is not a valid name.")
    print("*** {} ***".format(err))
else:
    print("This is a valid name! The new product name is: {}".format(product_idea)) #+ "inator" not working and it makes me sad.

#Working code below:

def suggest(product_idea):
    return product_idea + "inator"#Works now with the changes below

try:
    product_idea = input("Please, enter a name: ")
    if len(product_idea) <= 3:
        raise ValueError("Names need to be more than 3 characters long.")
except ValueError as err:
    print("That is not a valid name.")
    print("*** {} ***".format(err))
else:
    ideas = suggest(product_idea) #Created this new variable
    print("This is a valid name!\nThe new product name is: {}".format(ideas))#+ "inator" works this way

3 Answers

Efaz Khan
Efaz Khan
5,194 Points

You can not call product_idea by it's own. It is just the parameter. The function suggest() is going to print/return the result. So you need to call the function and pass its parameter.

Efaz Khan
Efaz Khan
5,194 Points

Hey, you need to pass the function suggest inside the format().

print("This is a valid name! The new product name is: {}".format(suggest(product_idea)))

Efaz Khan, thank you. I just removed the name variable all together and passed the function, like you suggested and it worked as well. I guess there are different approaches and yours is shorter. I like it.

So, to be clear, when defined functions have an addition, like + "inator", I should pass the whole function? This might be particular to this challenge but I just wanted to understand the reasoning behind why I should pass the whole function, at least in this particular case and if it's something that should apply regardless.

Thanks again!

Now, the code looks like this:

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

try:
    product_idea = input("Please, enter a name: ")
    if len(product_idea) <= 3:
        raise ValueError("Names need to be more than 3 characters long.")
except ValueError as err:
    print("That is not a valid name.")
    print("*** {} ***".format(err))
else:
    print("This is a valid name!\nThe new product name is: {}".format(suggest(product_idea)))
#Removed the ideas variable and passed the suggest(product_idea) function

Efaz Khan, thank you VERY much! I understand it now.

Have a great day!