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

Print Hello Name

Write a function to return either "Hello, [name]!" or "Hello there!" based on the user input.

Here is what I came up with. Where could I be going wrong?

def say_hello(name): name = input("What is your name? ") if name == input(name): return("Hello," + name + "!" ) else: return("Hello there!")

2 Answers

Hi there,

Just to make it easier to read, when you paste your code in future, if you add three "back ticks" followed the the word python like this (I've put 3 apostrophe's instead so you can see what I mean):

'''python

your code here

'''

Then your code will appear like this:

def say_hello(name):
    name = input("What is your name? ")
    if name == input(name):
      return("Hello," + name + "!" )
    else: 
      return("Hello there!")

Anyhoo, down to the matter at hand. There's two little problems here. The first is the if statement. If we follow the path of that name variable:

  • it comes in to our function with whatever value the user gives it.
  • then we call the input() function and capture what's written by the user
  • we then overwrite the name variable with that input
  • Now we get to the if statement and call input again. The user is again prompted for input and we check if it's the same as the last thing they wrote.

To help, I believe the question is asking to return "hello Bob" if the user types bob, and "hello there" if the user doesn't type anything.

Finally one little tip: does your function need any arguments?

Its working fine for me.

If there is an error make sure your indentations are correct(try using markdown cheatsheet when you question/answer/comment).

What you are doing is :

1> Get argument name(which is never used);

2> Input from user and save in var 'name';

3> check if name is equal to the value to again taken input from user.(you are taking input two times.)
def say_hello(name): 
    name = input("What is your name? ") 
    if name == input(name): 
        return("Hello," + name + "!" ) 
    else: 
        return("Hello there!")

command prompt content:

>>> say_hello("mark")
What is your name? MARK
MARKMARK
'Hello,MARK!'
>>>

in third line one MARK is output and following mark in an input again for the line name==input(name).

Hope this helps.