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 trialDanielle Murray
Python Development Techdegree Graduate 7,755 PointsHi 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")
4 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsHey 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
thenjoin
, the objectans
needs to be an empty list [], not a string. - Since, โstringโ is from removed
def
line, thefor
loop needs to useword
- 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!!!
Danielle Murray
Python Development Techdegree Graduate 7,755 PointsHi 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
Treehouse Moderator 68,441 PointsThe 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.
Danielle Murray
Python Development Techdegree Graduate 7,755 PointsHow 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
Treehouse Moderator 68,441 PointsAll the code should be inside the morse_code
function.
def morse_code(word):
# ...
ans ...
for char...
ans.append....
return ...
remember:
- no space before this paren
append(
- be sure to
join
using the proper character. - not space
Danielle Murray
Python Development Techdegree Graduate 7,755 PointsHi 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.
Danielle Murray
Python Development Techdegree Graduate 7,755 PointsThank you so much Chris Freeman it worked! Thank you.
Chris Freeman
Treehouse Moderator 68,441 PointsGreat! Way to stick with it! Feel free to tag me on other questions!
Danielle Murray
Python Development Techdegree Graduate 7,755 PointsDanielle Murray
Python Development Techdegree Graduate 7,755 PointsI've even used the hints but they are of no help neither are the lecture videos.