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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Mohammed Saleh](https://secure.gravatar.com/avatar/c9042abfb876a686e91634aec57de1cc?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Mohammed Saleh
Python Development Techdegree Student 2,989 PointsLet's Get Together task.
Use the function called morse_code. It will take 1 parameter, a word. For example: 'apple'.
Using the given morse coe
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'
}
return ['a']['p']['p']['l']['e']
morse_code(apple)
can someone tell me in another simple words what are we suppose to do here, because i didn't get the question from the first place ? thanks
2 Answers
![jb30](https://secure.gravatar.com/avatar/681668d7cc4c5486851384a0feddda37?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
jb30
44,806 PointsIf the input word is a
, the challenge wants you to return the morse code representation of a
, which is given to you in morse_dict
as 'dot-dash'
.
If the input word is ab
, the challenge wants you to return a string with the morse code representation of a
, followed by a -
, followed by the morse code representation of b
, which is given to you as 'dash-dot-dot-dot'
, so the string to return would be 'dot-dash-dash-dot-dot-dot'
.
If the input word is abc
, the challenge wants you to return the morse code representation of a
, followed by a -
, followed by the morse code representation of b
, followed by a -
, followed by the morse code representation of c
, which is given to you as 'dash-dot-dash-dot'
, so the string to return would be 'dot-dash-dash-dot-dot-dot-dash-dot-dash-dot'
.
![Mohammed Saleh](https://secure.gravatar.com/avatar/c9042abfb876a686e91634aec57de1cc?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Mohammed Saleh
Python Development Techdegree Student 2,989 Pointsthank you so much for you great explanation
i put this code :
for character in morse_dict: character += morse_dict['n'] + "-" character += morse_dict['b']
return character
but always gets :
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'. Instead, it was adash-dot-dash-dot-dot-dot.
from where the "a " before the first dash comes from.
![jb30](https://secure.gravatar.com/avatar/681668d7cc4c5486851384a0feddda37?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
jb30
44,806 PointsThe first "a" comes from the key 'a'
in the dictionary morse_dict
.
Assuming that the line for character in morse_dict:
is your only line not indented, you currently have
for character in morse_dict: # loops through each key of the dictionary, starting with character = 'a' since dictionaries are ordered
character += morse_dict['n'] + "-" # character = 'adash-dot-'
character += morse_dict['b'] # character = 'adash-dot-dash-dot-dot-dot'
return character # returns 'adash-dot-dash-dot-dot-dot' and prevents the for loop from looping again
Your code does not currently use the function's input word
, so it will return the same string each time.