Urgent!HELP ME, PLEASEEEE!!

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2007
Posts: 48
Reputation: eleonora is an unknown quantity at this point 
Solved Threads: 1
eleonora eleonora is offline Offline
Light Poster

Urgent!HELP ME, PLEASEEEE!!

 
0
  #1
Nov 10th, 2007
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,
Eleonora
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Urgent!HELP ME, PLEASEEEE!!

 
0
  #2
Nov 10th, 2007
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.

Jeff
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 48
Reputation: eleonora is an unknown quantity at this point 
Solved Threads: 1
eleonora eleonora is offline Offline
Light Poster

Re: Urgent!HELP ME, PLEASEEEE!!

 
0
  #3
Nov 11th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,042
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 933
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Urgent!HELP ME, PLEASEEEE!!

 
0
  #4
Nov 11th, 2007
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 ...
  1. # months abbreviated dictionary (created from a list)
  2. mad = {'MAR': 3, 'FEB': 2, 'AUG': 8, 'SEP': 9,
  3. 'APR': 4, 'JUN': 6, 'JUL': 7, 'JAN': 1,
  4. 'MAY': 5, 'NOV': 11, 'DEC': 12, 'OCT': 10}
  5.  
  6. date = "25-DEC-1970"
  7. date_list = date.split('-')
  8.  
  9. # note that the month abbreviation is at index 1
  10. print date_list # ['25', 'DEC', '1970']
  11.  
  12. # make sure case is consistent with dictionary, all upper case
  13. month_abbrev = date_list[1].upper()
  14.  
  15. # now get the value from the dictionary (giving it the key)
  16. month_number = mad[month_abbrev]
  17.  
  18. # test it ...
  19. print date, "-->", month_number # 25-DEC-1970 --> 12
  20.  
  21. # the other numbers ...
  22. day_number = int(date_list[0])
  23. year_number = int(date_list[2])
  24.  
  25. 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.
Last edited by vegaseat; Nov 11th, 2007 at 2:10 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 48
Reputation: eleonora is an unknown quantity at this point 
Solved Threads: 1
eleonora eleonora is offline Offline
Light Poster

Re: Urgent!HELP ME, PLEASEEEE!!

 
0
  #5
Nov 11th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,042
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 933
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Urgent!HELP ME, PLEASEEEE!!

 
0
  #6
Nov 11th, 2007
Writing and reading a text file is easy with Python ...
  1. mytext = "this is my text I want to write to a file"
  2.  
  3. filename = 'test.txt'
  4.  
  5. # open the file for writing
  6. fout = open( filename, 'w' )
  7. # write text to file
  8. fout.write(mytext)
  9. # close the file
  10. fout.close()
  11.  
  12. # read it back in to check if it has been written properly ...
  13. fin = open(filename, 'r')
  14. text = fin.read()
  15. fin.close()
  16.  
  17. print '-'*70
  18. print text
  19. print '-'*70
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 48
Reputation: eleonora is an unknown quantity at this point 
Solved Threads: 1
eleonora eleonora is offline Offline
Light Poster

Re: Urgent!HELP ME, PLEASEEEE!!

 
0
  #7
Nov 12th, 2007
Thank you very much!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC