Inspired over the weekend, I updated my first program (
hello.py) with an extra
raw_input line so you could actually see the results before the program exited (a problem I noticed when used Nautilus to run it in the terminal, rather than running it in from already open terminal). Here’s
#!/usr/bin/env python # hello.py v1.1 # (8 Aug 2010) # My first program! name = raw_input("What is your name? ") print "Hello, " + name + "!" raw_input("Press to exit.")
You can also download
hello.v1-1.py from my Dropbox. Not satisfied with a mere one-line revision, I tackled my next project: a program that could take numerical date input and convert it to a human-readable “January 1st, 2010” American date style. Here’s my first take on the idea,
#!/usr/bin/python # date.py v1.0 # 8 Aug 2010 # My second program # user instructions print 'Convert your numerical dates to \"January 1st, 2010\" style.' # month list month_list = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] # sets suffixes for 31 days as 1st, 2nd, 3rd, 4th ... 20th, 21st, etc. day_suffix = ['st','nd','rd'] + 17*['th'] \ + ['st','nd','rd'] \ + 7*['th'] + ['st'] # get user input year = raw_input('Year (0-present): ') month = raw_input('Month (1-12): ') day = raw_input('Day (1-31): ') # convert user input to integer month_int = int(month) day_int = int(day) # format user input month_name = month_list[month_int-1] day_ordinal = day + day_suffix[day_int-1] # print results onscreen print 'Your date is: ' + month_name + ' ' + day_ordinal + ', ' + year raw_input('Press <enter> to exit...')
You can also download
date.py from my Dropbox. I wanted to take the book’s idea and run with it completely on my own, but the concept of lists in Python semi-baffled me, so I kept having to look at the book to actually get it done 😡 . Everything else made sense, but lists still baffled me, so I thought, Why not try to do something that’s not in the book? So I made
#!/usr/bin/python # date.py v1.0 # 8 Aug 2010 # My second program # user instructions print 'Convert MM/DD/YYYY dates to \"January 1st, 2010\" style.' # month list month_list = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] # sets suffixes for 31 days as 1st, 2nd, 3rd, 4th ... 20th, 21st, etc. day_suffix = ['st','nd','rd'] + 17*['th'] \ + ['st','nd','rd'] \ + 7*['th'] + ['st'] # get user input mmddyyyy = raw_input('Enter date in MM/DD/YYYY format: ') # convert user input to integer month_str = mmddyyyy[0:2] day_str = mmddyyyy[3:5] year_str = mmddyyyy[6:10] # format user input month_name = month_list[int(month_str)-1] day_ordinal = day_str + day_suffix[int(day_str)-1] # print results onscreen print 'Your date is: ' + month_name + ' ' + day_ordinal + ', ' + year_str raw_input('Press <enter> to exit...')
And yes, you can download
date.v1-1.py from my Dropbox 😉 . It does the same thing as v.1-0 output-wise, but v.1-1 asks the user for a MM/DD/YYYY formatted date to convert. I wanted to make it pick the numerical elements only, then format those numbers (would it be more elegant to strip the “/” elements first, because I’m not sure how to do that just yet — maybe convert the whole
raw_input string to an integer?), but honestly, lists are even more baffling to me now.
I thought I had it down — I figured that I should choose two numbers as a range (not three) for the MM and DD fields, and four (not five) for the YYYY field. I was wrong, and I really have no idea why at this point.
I will investigate more later; I want to try out several ways to convert
raw_input to Python-usable data, and perhaps give more than one result (American date style, European date style, Stardate style, whatever) at the same time, or have a menu structure… but for now I dream.
UPDATE: You know, I talked all about “the book” here, but I never told which book I was using! So, for all interested parties, I am using Magnus Lie Hetland’s Beginning Python: From Novice to Professional, which is a totally awesome resource so far. I found out that Apress made a second edition in 2008 which also covers Python 3, but as I’m starting with learning Python 2.6, I don’t think it’s a big deal. I would like to get that second edition sometime, though 😥 . My goal is to eventually go through several “Learning Python” type of books before I tackle larger projects, both to get more practice in, and also to learn Python from more than one viewpoint.
I also noticed that this WordPress template cuts off blockquoted <pre> code. I don’t know of any way to fix this, but the text is still there, and you can copy-paste it with no difficulty — you just can’t see anything past the margins. I found a much nicer way to post source code on WordPress.com.
UPDATE #3: Added screenshots.