I am a python newbie and i have trouble with those 2 exercises:

1.A date of the form "8-MAR-85" includes the name of the month, which must be translated to a number. Create a dictionary suitable for decoding month names to numbers. I have to create a function which uses string operations to split the date into 3 items using the "-" character. Then i have to translate the month, correct the year to include all of the digits. At the end, the function will accept a date in the "dd-MMM-yy" format and respond with a tuple of (y, m, d).

2.i have to create a function that will read from a text file and put words of it in random order and create in that way a poem(it will ask the user how many lines iit will be and how many words in each line). at the end it will ask the user whether to save it to a text file or to call the function again.

Please could you help me?PLEASEEEEE!
Thanks in advance,

Recommended Answers

All 6 Replies

With both programs, the place to start is with a couple of examples: "8-MAR-85", "12-DEC-1970", etc. and

(a) work them out by hand,
(b) describe to yourself how you did it, and
(c) write code to do the same.

That's how I write my code.


Could you please be more specific ? Especially for the exercise with months. I tried to solve the first exercise but I did this:

def month2number(ddmmmyy):
    dictionary = {'Jan':1,'Feb':2,'Mar':3....}
    split = ddmmmyy.split('-')
and I don't know how to continue! Could you please help me?

Thanks again!

Well, it is our policy on the forum not to do homework for you, but here are some elaborations (tests) that should help you in a hintful way ...

# months abbreviated dictionary (created from a list)
mad = {'MAR': 3, 'FEB': 2, 'AUG': 8, 'SEP': 9, 
    'APR': 4, 'JUN': 6, 'JUL': 7, 'JAN': 1, 
    'MAY': 5, 'NOV': 11, 'DEC': 12, 'OCT': 10}

date = "25-DEC-1970"
date_list = date.split('-')

# note that the month abbreviation is at index 1
print date_list  # ['25', 'DEC', '1970']

# make sure case is consistent with dictionary, all upper case
month_abbrev = date_list[1].upper()

# now get the value from the dictionary (giving it the key)
month_number = mad[month_abbrev]

# test it ...
print date, "-->", month_number  # 25-DEC-1970 --> 12

# the other numbers ...
day_number = int(date_list[0])
year_number = int(date_list[2])

print day_number, month_number, year_number  # 25 12 1970

I assume this is an example from the last century, so when your year_number is less than 100 simply add 1900 to it.

Thank you very much! I'm done with the first problem! I appreciate your help! But for the second exercise I still have a problem how to save the output to a text file...could you give me a clue please?

Writing and reading a text file is easy with Python ...

mytext = "this is my text I want to write to a file"

filename = 'test.txt'

# open the file for writing
fout = open( filename, 'w' )
# write text to file
# close the file

# read it back in to check if it has been written properly ...
fin = open(filename, 'r')
text = fin.read()

print '-'*70
print text
print '-'*70

Thank you very much!!

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.