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

Noor Hafiza Binti Ibrahim
Noor Hafiza Binti Ibrahim
11,712 Points

Use the function called morse_code. It will take 1 parameter, a word. For example: 'apple'. Using the given morse code

Bummer: Uh oh, I didn't get the correct value returned. It should have been dash-dot-dash-dot-dot-dot for the word 'tree

join.py
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'
    }
    # enter your code below
def convert_to_morse(word):
    word = word.lower()
    encoded_word = ""
    for character in word:
        encoded_word += morse_dict[character] + " "
    return encoded_word

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hey Noor Hafiza Binti Ibrahim, you are very close!

  • no need to define a new function. Put all of the code within the given morse_code function
  • there is an issue in accumulating the characters. The characters should be joined with a dash (-) instead of a space. Unfortunately, simply changing the assignment to encoded_word to add a trailing dash would leave an extra dash at the end. This could be fixed by returning encoded_word[:-1] to trim the dash but thereโ€™s a better way.

Strings are not mutable. Using string += new_bits creates a new string each time.

Use a list to accumulate characters, then use โ€-โ€œ.join() on the list to get the final string.

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

Noor Hafiza Binti Ibrahim
Noor Hafiza Binti Ibrahim
11,712 Points
    morse_code = ""

    for x in word:
        morse_code += morse_dict[x.lower()]
    return morse_code

#it raised an AssertionError: โ€˜dashdot-dash-dotdotdotโ€™ != โ€˜dash-dot-dash-dot-dot-dotโ€™
Noor Hafiza Binti Ibrahim
Noor Hafiza Binti Ibrahim
11,712 Points
#Another variation i tried:

    word = word.lower()
    encodedWord = ""
    for character in word:
        encodedWord += morse_dict[character] + "-"
    return encodedWord

 #still raised an AssertionError: โ€˜dash-dot-dash-dot-dot-dot-โ€˜ != โ€˜dash-dot-dash-dot-dot-dotโ€™
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Notice the trailing dash in your last attempt, as mentioned in my answer above.

Noor Hafiza Binti Ibrahim
Noor Hafiza Binti Ibrahim
11,712 Points

Got it!๐Ÿ˜Š it passes after

return encodedWord[:-1]

thanks