On 01/28/2014 11:46 PM, Michael L. Pierre wrote:
I am a newbie with Python (programming in general) and I am trying to create a
program that will take user name and dob and pump out their age, while on a
person's birthday the output not only states their name and age, but also
prints out ***HAPPY BIRTHDAY***
I have gotten it resolved to the point that it understands leap years and
gives the correct age. My problem arises when a date is input that is the
current month, but a future date (i.e. today's date is 1/28/2014 but the input
dob is 1/30/1967) It skips over the elif option to subtract one year and prints
out ***HAPPY BIRTHDAY***
I am only going to paste the non-leap year code, because the leap year code is
basically identical.
#Leapyear calculations/decision
if leap_year != int:
age_month = int(current_split[1]) - int(dob_split[1])
#print age_month
if age_month != 0:
month_less = 1
age = int(current_split[0]) - int(dob_split[0]) - month_less
print "you are", age, "years old"
elif age_month == 0 and int(current_split[2]) > int(dob_split[2]):
age = int(current_split[0]) - int(dob_split[0]) - month_less
print "you are", age, "years old"
else:
age = int(current_split[0]) - int(dob_split[0])
print "You are", age, "and today is your birthday ***HAPPY BIRTHDAY***"
Any help would be greatly appreciated :)
This is not an answer to your question, just some notes (which may help for this
issue and others). The number one problem in programming is certainly
understandability, or clarity for short. Programming is very hard (and quality
very low) because we have major problems to understand what the code *actually*
means, even often our own code while we're at it (not to mention a few weeks
later, or years). We should do our best to reduce complication and obscurity as
much as possible (even to the point of reducing our ambitions in terms of scope
or scale, functionality or sophistication).
* What about a comment explaining your logic here, also for yourself, in plain
natural language? (obviously it's not obvious, firstly for yourself, else the
bug would be obvious...)
* I cannot guess what "if leap_year != int" may mean. (But I note you know,
apparently, that int is a python type and int() acts like a function producing
an int value.)
* You are using items of multi-item data 'current_split' and 'dob_split'
(probably tuples) as key elements in the control of your application logic: why
about naming these elements after their *meaning*? This would make the flow
control clear, your understanding better, and your debugging, modifications,
maintenance far easier? eg for instance
year, month, day = current_split
[Or better create a Date type with year, month, day properties (or use python's,
in the module datetime).]
* It's certainly acceptable to name something 'dob' in code (provided you
comment it), but not in the text of a message on a mailing. (For whatever
mysterious reason the meaning popped up in mind nevertheless, so _i_ don't need
a translation anymore.)
* You don't need the "age_month == 0" sub-condition in the elif branch. (why?)
(or your logic is wrong otherwise)
d
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor