I am relatively new to python and am having a hard time with an assignment. We just started using dictionaries and I can't quite grasp what is going on. I am just having trouble figuring out where to even start exactly. This is the code my teacher gave me and he wants us to build off of it

def buildMorseDicts(morseFile):
    '''build and return dictionaries to translate to and from Morse code'''
    toMorse = {}
    fromMorse = {}
    # finish this code then remove this comment
    
    
    return (toMorse, fromMorse)

def encode(word,toMorse):
    '''creates a string of . and _ characters to encode each character
    in word to Morse code using the toMorse dictionary.  A space is placed
    between each string of . and _ that represent a Morse character in 
    the encoded version.'''
    morse = ""
    # finish this code then remove this comment
    
    
    return morse
    
def decode(morseWord,fromMorse):
    '''creates a string of english letters that are the decoded version of
    each space-separated sequence of . and _ characters in morseWord string.'''
    english = ""
    # finish this code then remove this comment
    
    
    return english
    
def main():
    # Open the file with the translation between regular characters and Morse code
    filObj = open("morse.txt", 'rU')
    toMorse, frMorse = buildMorseDicts(filObj)
    filObj.close()  # Close the file when finished with it.
    
    # Open the file to translate from text to Morse code
    filObj = open("morse1.txt", 'rU')
    for word in filObj:
        print encode(word,toMorse)
    filObj.close()  # Close the file when finished with it.
    
    # Open the file to translate from text to Morse code
    filObj = open("morse2.txt", 'rU')
    for word in filObj:
        print decode(word,frMorse)
    filObj.close()  # Close the file when finished with it.

main()

Function 1:
read in morse file (was passed as argument)
create dict fromMorse: example {".-":"a" , "-...":"b" , etc...}
creat dict toMorse: example {"a":".-" , "b":"-..." , etc...}
return both as tuple

Function 2:
I don't know the contents of the .txt
using arguments supplied
map the word to the dict value
return morse code as string

Function 3:
same as function 3, but in reverse
return English string

Function 4:
This is the main program
This should work if you did the other functions correctly


That should be a good starting point. Work on it and if you are still having problems, post what you have so far, and we will continue to help.

i want more info on this one. its due midnight so hurry!

I'm not going to do it for you. I gave you enough information to do it.
I will point you to where you can get more info on what needs to be done though.

You can find all you need here.

I have the first function down

def buildMorseDicts(morseFile):
	'''build and return dictionaries to translate to and from Morse code'''
	toMorse = {}
	fromMorse = {}
	for line in filObj:
		(key, value) = line.split()
		toMorse[key] = val
		fromMorse[val] = key
	return (toMorse, fromMorse)

I am on the 2 others, the ones that actually do the translating. Figuring out what they are supposed to be doing is easy (I think).
1.) It has to read, character by character, the english word
2.) Switch the english characters with the correct morse sequence
3.) return that

I just dont really know how to code that, I am thinking something like

for word in toMorse:
                word = word.upper() # no lowercase in the files
		do stuff
                do more stuff

Can someone help me out? I dont care about the deadline for this assignment anymore, I just really want to know how this language works...

I should be more clear

def encode(word,toMorse):
	'''creates a string of . and _ characters to encode each character
	in word to Morse code using the toMorse dictionary. A space is placed
	between each string of . and _ that represent a Morse character in
	the encoded version.'''
	morse = ""
	# finish this code then remove this comment
	for word in toMorse:
	# NOT DONE YET
	return morse

is where I am at, if I can get this going from morse to english should be easy. I think I need to use a for loop but I am not quite sure how to get word to compare to the toMorse dictionary and return a new string of "."s and "-"s.

Also, I guess this is where others in my class have come to find help on these assignments, shame I didn't ask the internet for help as well. And I am not looking for this to be done for me, I want to be pushed in the right direction to figure it out, I want to learn this stuff

Do you have an example of what is in the toMorse dict?

In the second function, you see it takes two arguments. One is the word you need to decode, the other is a dict of the mapping values. hence def encode(word,toMorse)

You are correct on needing to loop through each character, you will use that char to use as the key in the dict.

def encode(word,toMorse):
    morse = ""
    for character in word:
        morse += toMorse[character]

Work with that.

The second function is exactly like this one, except we are going from morse to english.

To elaborate on the "for" loop, it loops through the given irritable:

x = "hello world"
for letter in x:
  print letter

OUTPUT

h
e
l
l
o

w
o
r
l
d

This is also true with anything that can be gone over, such as lists and tuples:

l = [4,5,6]
t = (7,8,9)

for element in l:
  print element

for element in t:
  print element

OUTPUT

4
5
6
7
8
9

ok, thank you, i am coming from C so some of this is a little wierd (not having to declare a variable first in order to use it keeps confusing me, but i will get used to that in time)

File "lab10_72.py", line 10, in buildMorseDicts
for line in filObj:
NameError: global name 'filObj' is not defined

so that must mean i made a mistake in this somewhere

def buildMorseDicts(morseFile):
	'''build and return dictionaries to translate to and from Morse code'''
	toMorse = {}
	fromMorse = {}
	for line in filObj:
		(key, value) = line.split()
		toMorse[key] = val
		fromMorse[val] = key
	return (toMorse, fromMorse)

I have had this problem in the past and how i fixed it was pull out that variable and declare it globally from the top (i think it is not in the right namespace?), but our professor said not to get in the habit of doing that. Is there another way of fixing this?

def buildMorseDicts(morseFile):
	'''build and return dictionaries to translate to and from Morse code'''
	toMorse = {}
	fromMorse = {}
	for line in filObj:
		(key, value) = line.split()
		toMorse[key] = val
		fromMorse[val] = key
	return (toMorse, fromMorse)

def encode(word,toMorse):
# still working on this
	return morse

def decode(morseWord,fromMorse):
# still working on this too
	return english


def main():
	# Open the file with the translation between regular characters and Morse code
	filObj = open("morse.txt", 'rU')
	toMorse, frMorse = buildMorseDicts(filObj)
	filObj.close() # Close the file when finished with it.
	
	# Open the file to translate from text to Morse code
	filObj = open("morse1.txt", 'rU')
	for word in filObj:
		print encode(word,toMorse)
	filObj.close() # Close the file when finished with it.
	
	# Open the file to translate from text to Morse code
	filObj = open("morse2.txt", 'rU')
	for word in filObj:
		print decode(word,frMorse)
	filObj.close() # Close the file when finished with it.

main()

that is where i am at and i am getting these errors
Traceback (most recent call last):
File "lab10_72.py", line 60, in <module>
main()
File "lab10_72.py", line 45, in main
toMorse, frMorse = buildMorseDicts(filObj)
File "lab10_72.py", line 10, in buildMorseDicts
for line in filObj:
NameError: global name 'filObj' is not defined

what am i doing wrong here?

Your not working with filObj yet.

You can see that the function takes a argument/parameter morseFile.

You will be operating on morseFile.

Example:

def printStuff(argument):
  for stuff in argument:
    print stuff

fileObj = open("test.txt", "r")
printStuff(fileObj)

You did not give the correct name, check the parameter of function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.