1
votes

This is probably very simple, but I am a beginner in python and I wanted to compare birthday dates by prompting a user to enter a date in MM-DD format. No year because year is current year (2011). It will then prompt the user to enter another date, and then the program compares it to see which one is first. Then it prints out the earlier day and it's weekday name.

Example: 02-10 is earlier than 03-11. 02-10 is on thursday and 03-11 is on friday

I just started learned modules and I know i'm supposed to use the datetime module, a date class and strftime to get the weekday name. I don't really know how to put it all together.

If someone can help me get started that would really help! I have some bits and pieces together:

 import datetime  

 def getDate():  

     while true:  
         birthday1 = raw_input("Please enter your birthday (MM-DD): ")  
         try:  
             userInput = datetime.date.strftime(birthday1, "%m-%d")  
         except:  
             print "Please enter a date"  
     return userInput

     birthday2 = raw_input("Please enter another date (MM-DD): ")

        if birthday1 > birthday2:  
            print "birthday1 is older"  
        elif birthday1 < birthday2:  
            print "birthday2 is older"  
        else:  
            print "same age"  
3
You have some problems with indentations, that's obvious. And why did you use try only once? You read two dates. I don't really understand your moves. You should first read a good Python-language tutorial. docs.python.org/tutorial/index.htmlMaciej Ziarko

3 Answers

4
votes

There are a few problems I can see in the code you've posted. I hope it will be helpful to point some of these out, and provide a somewhat rewritten version:

  • Indentation is broken, but I guess this might just be problems pasting it into Stack Overflow
  • strftime is for formatting times, not parsing them. You want strptime instead.
  • In Python, True has a capital T.
  • You're defining the getDate function but never using it.
  • You would never exit your while loop, since you don't break after getting the input successfully.
  • It's considered bad style to use "camel case" for variable and method names in Python.
  • You use the word "older" in reference to the dates, but without a year you can't say if one person is older than the other.
  • You catch any exception thrown when you try to parse the date, but don't display it or check its type. That's a bad idea since if you've mistyped a variable name (or some similar typo) on that line, you won't see the error.

Here's a rewritten version of your code that fixes those problems - I hope it's clear from the above why I made those changes:

import datetime  

def get_date(prompt):
    while True:
        user_input = raw_input(prompt)  
        try:  
            user_date = datetime.datetime.strptime(user_input, "%m-%d")
            break
        except Exception as e:
            print "There was an error:", e
            print "Please enter a date"
    return user_date.date()

birthday = get_date("Please enter your birthday (MM-DD): ")
another_date = get_date("Please enter another date (MM-DD): ")

if birthday > another_date:
    print "The birthday is after the other date"
elif birthday < another_date:
    print "The birthday is before the other date"
else:  
    print "Both dates are the same"
1
votes

There are two main functions that are used for converting between a date object and a string: strftime and strptime.

strftime is used for formatting. It returns a string object. strptime is used for parsing. It returns a datetime object.

More info in the docs.

Since what you want is a datetime object, you would want to use strptime. You can use it as follows:


>>> datetime.datetime.strptime('01-23', '%m-%d')
datetime.datetime(1900, 1, 23, 0, 0)

Note that not having the year being parsed will set the default to 1900.

1
votes

Well, datetime.date.strftime requires datetime object instead of string.

In your case the best thing is to create date manually:

import datetime
...
birthday1 = raw_input("Please enter your birthday (MM-DD): ")
try:
  month, day = birthday1.split('-')
  date1 = datetime.date(2011, int(month), int(day))
except ValueError as e:
  # except clause
# the same with date2

And then when you have two dates, date1 and date2, you can just do:

if d1 < d2:
  # do things to d1, it's earlier
else:
  # do things to d2, it'2 not later