I've been programming for all of about 5 hours in Python now. From first opening my browser and googling the term to making this post here. I've written something up too! It's a short script that accepts input from the user about their date of birth and just prints some statistics about it. Such as their age in days/months/years.
It has some basic error checking though it's nothing serious like checking whether or not the DAY part of the DATE is possible in that MONTH (i.e. so you can get the 31st of February and it will accept it). Though it does make sure you don't make a 13th month.
Please take a look and I'm particularly interested if anyone can tell me alternate ways of doing things that I've done, especially if it's more efficient (or just generally better).
Thanks in advance.
# Filename: age2.py # Author: Mark Williams #Do all the module imports first import datetime #Just a Def to all up to convert strings to UPPERCASE def to_upper(string): upper_case = "" for character in string: if 'a' <= character <= 'z': location = ord(character) - ord('a') new_ascii = location + ord('A') character = chr(new_ascii) upper_case = upper_case + character return upper_case #End of Def here. #Next we'll import the date from that module we loaded. from datetime import date #And put the date today in a variable now = date.today() #Then we'll make a seperate variable for each 'piece' #of the date for manipulation #Yeah, YEAR input now with error checking error_var = 0 while error_var == 0: y = int(raw_input('Please enter the YEAR of which you were born: ')) if y >= 2009: print "Error, you're not born yet! Try again." elif y < 0: print "Error, no way we're you born BC!" else: error_var = 1 #Next is Month input with error checking for both integer input #and for string input. error_val = 0 while error_val == 0: m = raw_input('Please enter the MONTH of which you were born: ') try: dummy = int(m) #error checking on the input number (as it was given as an integer) m = int (m) if m < 1 or m > 12: print "Error? That's not a month!" else: error_val = 1 except: m = to_upper(m)[:3] #Convert the string version to an integer version #Lots of "error_val..." here, might take a look at #removing these somehow. if m == "JAN": m = 1 elif m == "FEB": m = 2 error_val = 1 elif m == "MAR": m = 3 error_val = 1 elif m == "APR": m = 4 error_val = 1 elif m == "MAY": m = 5 error_val = 1 elif m == "JUN": m = 6 error_val = 1 elif m == "JUL": m = 7 error_val = 1 elif m == "AUG": m = 8 error_val = 1 elif m == "SEP": m = 9 error_val = 1 elif m == "OCT": m = 10 error_val = 1 elif m == "NOV": m = 11 error_val = 1 elif m == "DEC": m = 12 error_val = 1 else: print "\nError, didn't get which month you meant there either \n\ enter it as an integer (1 to 12) or type the month name out, \n\ either as an abbreviation (Jan, Feb, Mar...) or in \n\ full (January, February...)\n" #Day input, again with error checking error_var = 0 while error_var == 0: d = int(raw_input('Please enter the DAY on which you were born: ')) if d < 1 or d > 32: print "Error, what DAY did you say?" else: error_var = 1 #Added here so as to not double commands d = int(d) m = int(m) y = int(y) #Put the input-ed date into a single variable birthday = date(y, m, d) #Work out the persons age age = now - birthday #Convert that to years (allowing for leap years too years = age.days / 365.25 #Convert it to months also months = age.days / 12 ## Output the data! print "\nToday, your are:\n\n" print age.days, " days" print months, " months" print "or" print int(years), " years old!" #Now just for fun we'll play with some list stuff #I'll try and make it look neat and 'readable' too. a = [\ 'January',\ 'February',\ 'March',\ 'April',\ 'May',\ 'June',\ 'July',\ 'August',\ 'September',\ 'October',\ 'November',\ 'December'] #Prepare 'm' for 'a' m = m - 1 #Add the 'st', 'nd', 'rd' or 'th' bit to the day! if d == 1 or d == 21 or d == 31: d = str(d) + 'st' elif d == 2 or d == 22 or d == 32: d = str(d) + 'nd' elif d == 3 or d == 23: d = str(d) + 'rd' elif d > 3 and d <= 20 or d >23 and d <= 30: d = str(d) + 'th' print "\n\nAlso I can tell you that you were born on: ", d, a[m], y #That's all folks!