| | |
Urgent!HELP ME, PLEASEEEE!!
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 48
Reputation:
Solved Threads: 1
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
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
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 ...
I assume this is an example from the last century, so when your year_number is less than 100 simply add 1900 to it.
python Syntax (Toggle Plain Text)
# 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
Last edited by vegaseat; Nov 11th, 2007 at 2:10 pm.
May 'the Google' be with you!
Writing and reading a text file is easy with Python ...
python Syntax (Toggle Plain Text)
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 fout.write(mytext) # close the file fout.close() # read it back in to check if it has been written properly ... fin = open(filename, 'r') text = fin.read() fin.close() print '-'*70 print text print '-'*70
May 'the Google' be with you!
![]() |
Similar Threads
- pls heeeeeeeeelp its urgent. (C)
- Urgent! (C++)
- URGENT: Need help on I/O code! (Java)
- Urgent help (Networking Hardware Configuration)
- Homepage keeps switching back to a porn site (Web Browsers)
- Importing SQL Script File - Urgent !! (Database Design)
Other Threads in the Python Forum
- Previous Thread: Need help getting text to display in a graphics window
- Next Thread: transparent image
| Thread Tools | Search this Thread |
accessdenied advanced aliased argv beginner bits calling casino change command convert count csv cturtle cursor def dictionary digital dynamic dynamically enter event examples external file float format frange function google gui hints homework i/o iframe import info input jaunty java keyboard lapse line linux list lists loop microphone mouse multiple newb number numbers obexftp output parameters parsing path port prime programming projects py py2exe pygame pygtk pyopengl python random recursion return scrolledtext signal skinning sprite stderr string strings subprocess table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable voip web-scrape whileloop windows wxpython






