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 trialMohammed 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
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
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
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.