From ee49fee0f500ea8adda0f780efe396788b4beb83 Mon Sep 17 00:00:00 2001 From: Dinko Galetic Date: Wed, 09 Jun 2010 21:54:13 +0000 Subject: Added validate_date(), which checks whether you entered a proper date. --- (limited to 'data') diff --git a/data/GSOC examples/birthday reminder b/data/GSOC examples/birthday reminder index 485136c..b66323a 100644 --- a/data/GSOC examples/birthday reminder +++ b/data/GSOC examples/birthday reminder @@ -112,6 +112,32 @@ def load_from_file(filename): # Return that to whoever called load_from_file() in the first place. return bday_dictionary +# This function checks whether the string you entered is actually a date. +# What are the things we check for? +# What could also be checked? Is it possible to enter a future date now? +def validate_date(user_input): + if len(user_input) != 8: + return False + + try: + int(user_input) + except: + return False + + if int(user_input) < 0: + return False + + if int(user_input[0:2]) > 31: + return False + + if int(user_input[2:4]) > 12: + return False + + if int(user_input[4:8]) > int(time.strftime("%Y")): + return False + + return True + # This function displays the menu which you see when you run the program. def menu(): # Here we will store all the birthday data. Notice the {} - that's how we @@ -153,14 +179,21 @@ def menu(): name = raw_input() print "Enter " + name + "'s birthday (ddmmyyyy): " birthday = raw_input() - add_birthday(name, birthday, birthdays) + if validate_date(birthday): + add_birthday(name, birthday, birthdays) + else: + print "That is not a valid birthday." elif choice == "5": print "Enter your friend's name: " name = raw_input() print "Enter " + name + "'s birthday (ddmmyyyy): " birthday = raw_input() - change_birthday(name, birthday, birthdays) + if validate_date(birthday): + change_birthday(name, birthday, birthdays) + else: + print "That is not a valid birthday." + elif choice == "6": print "Enter your friend's name: " -- cgit v0.9.1