diff options
author | Dinko Galetic <dgaletic@everflame.(none)> | 2010-06-09 21:54:13 (GMT) |
---|---|---|
committer | Dinko Galetic <dgaletic@everflame.(none)> | 2010-06-09 21:54:13 (GMT) |
commit | ee49fee0f500ea8adda0f780efe396788b4beb83 (patch) | |
tree | d36e0db079fe0a1f09900dfbd50f0efae475497b | |
parent | 2ab26284b6b1000fbae0ac84844dc4557de6d000 (diff) |
Added validate_date(), which checks whether you entered a proper date.
-rw-r--r-- | data/GSOC examples/birthday reminder | 37 |
1 files changed, 35 insertions, 2 deletions
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: " |