I'll preface this by saying that I'm fairly new to Python (after having 2-3 years of C++ under my belt), but for some reason I can't figure out the reasoning behind global variables and when/how I should be using them.
For instance, there is a project for school that we've been working on for the past week. Basically we need to compute the number of days between a birthday and the next birthday (mind you, WITHOUT using the built-in Python modules for date/time).
I'm perfectly fine with figuring out the ordinal date (how many days into a year a specific date is), leap years, as well as getting the inputs from the user. However, when I try and tie it all together.....it falls apart quick.
For instance, here is what I've got so far:
Jan = 0 Feb = 31 Mar = 59 Apr = 90 May = 120 Jun = 151 Jul = 181 Aug = 212 Sep = 243 Oct = 276 Nov = 305 Dec = 334 dTilBday = 0 curCount = 0 birCount = 0 curMonth = input('Enter the current month in the Mmm (ex Jan) format:') curDay = input('Enter the current day in the DD format:') curYear = input('Enter the current year:') birMonth = input('Enter your birth month in the Mmm (ex Jan) format:') birDay = input('Enter your birth day in the DD format:') def isLeapYear(curYear): if (curYear % 4 == 0 and curYear % 100 != 0) or (curYear % 400 == 0): return 1 else: global Mar Mar = 60 global Apr Apr = 91 global May May = 121 global Jun Jun = 152 global Jul Jul = 182 global Aug Aug = 213 global Sep Sep = 244 global Oct Oct = 277 global Nov Nov = 305 global Dec Dec = 335 return 0 def daysTilBday(): if birCount > curCount: global dTilBday dTilBday = birCount - curCount else: dTilBday = (birCount + 365) - curCount return 0 def BirthdayPrintout(): if birCount == curCount: print 'Congrats, today is your birthday!' else: print 'There are', dTilBday(), 'days til your next birthday' return 0 curCount = curMonth + curDay birCount = birMonth + birDay
What I'm trying to do is:
A) assign starting values for days in a month
B) read user input
C) determine if the year is a leap year
C)ii modify the original days in a month values if it is a leapyear
D) calculate the ordinal date
E) calculate the days between the current date and the birthdate using the ordinal dates for each
F) simple printout saying the total days til birthday, or saying "Happy Birthday" if they are the same date
I've tried going to office hours, but I'm still INCREDIBLY confused by the global values :(
Any help is GREATLY appreciated!