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 Functions, Packing, and Unpacking Getting Info In and Out of Functions Functions with Arguments and Returns

what is wrong with my code?

I am pretty sure this is the correct syntax or am I wrong?

creating_functions.py
def hello_student(name):
    print('Hello' + name)
Justin Cox
Justin Cox
12,123 Points

While you can technically use this code, it will only work when name is guaranteed to be a String. If you pass in an Integer, for example, it will fail saying you cannot concatenate integers with strings.

You have a few options for a more pythonic way (and less user-error prone):

Using string interpolation:

def hello_student(name):
  print('Hello {}'.format(name))

hello_student('Bob')

Or using F-Strings (only supported in Python 3.6 or higher),

def hello_student(name):
  print(f"Hello {name}")

hello_student('Bob')

If you absolutely want to stick to using the + sign, you could try casting name to a string inside your definition, like so:

def hello_student(name):
    str_name = str(name)     # convert name to string
    print("Hello " + name)   # two strings will concatenate

hello_student('Bob')

I hope this helps!

1 Answer

boi
boi
14,242 Points

The challenge wants you to return the value not print, also the challenge wants to replicate exactly what has been described anything that does not match, even a missing space or a period is considered a sin by the Gods, and it seems you are missing space in between. a major violation in TreeHouse

def hello_student(name):
    return('Hello ' + name) #return and space before the closing comma 'Hello '

oh right that makes it much clearer. Thank you