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!

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 Technical Interview Prep: Python Basics Basic Python Let's Get Together

Hi could you please help me and give me more feedback regarding my code for this, I'm really struggling with this code.

I've even watched you tube tutorials on this but I don't seem to be getting it right, I am getting a SyntaxError.

def morse_code(word):
    morse_dict = {
        'a': 'dot-dash',
        'b': 'dash-dot-dot-dot',
        'c': 'dash-dot-dash-dot',
        'd': 'dash-dot-dot',
        'e': 'dot',
        'f': 'dot-dot-dash-dot',
        'g': 'dash-dash-dot',
        'h': 'dot-dot-dot-dot',
        'i': 'dot-dot',
        'j': 'dot-dash-dash-dash',
        'k': 'dash-dot-dash',
        'l': 'dot-dash-dot-dot',
        'm': 'dash-dash',
        'n': 'dash-dot',
        'o': 'dash-dash-dash',
        'p': 'dot-dash-dash-dot',
        'q': 'dash-dash-dot-dash',
        'r': 'dot-dash-dot',
        's': 'dot-dot-dot',
        't': 'dash',
        'u': 'dot-dot-dash',
        'v': 'dot-dot-dot-dash', 
        'w': 'dot-dash-dash',
        'y': 'dash-dot-dash-dash',
        'z': 'dash-dash-dot-dot'
def morsify(string):
    ans = ' '
    for char in string:
        ans.append (morse_dict[char])
        return "".join(ans)
print morsify("CAT")

I've even used the hints but they are of no help neither are the lecture videos.

4 Answers

Chris Freeman
Chris Freeman
Treehouse Moderator 68,404 Points

Hey Danielle Murray, you’re on the correct path, here are the fixes:

  • The challenge wanted you to expand the current function and not create a new function of your own. So the def line can be removed.
  • To use append then join, the object ans needs to be an empty list [], not a string.
  • Since, β€œstring” is from removed def line, the for loop needs to use word
  • the join string needs to be a dash - instead of an empty char.
  • the print statement needs removal as it is flagged by the checker.

Post back if you need more help. Good luck!!!

Hi Chris Freeman i made these changes but am i still missing something because i am only getting dash as the output instead of dash-dot-dash-dot-dot-dot.

This is my code:

ans = []
for char in word:
      ans.append (morse_dict[char])
      return " ".join(ans)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,404 Points

The return is indented into the for loop. This causes the loop to return at the end of the first iteration. Decrease the indentation of the return statement so the for loop can complete.

How do I indent the return statement properly and what else is wrong with my code? I've been stuck on this task for very long.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,404 Points

All the code should be inside the morse_code function.

def morse_code(word):
    # ...
    ans ...
    for char...
    return ...


  • no space before this paren append(
  • be sure to join using the proper character. - not space

Hi Chris Freeman No matter how i indent the return line it gives me back that my return statement is outside of the for loop. I feel like I am making a very simple mistake that I'm missing but I don't understand why.

Thank you so much Chris Freeman it worked! Thank you.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,404 Points

Great! Way to stick with it! Feel free to tag me on other questions!