Reputation: 1
You could call me an extreme newbie, I have a course at school for which I need to be able, sort of, to solve problems using python. All so far have worked, but the last one (just a small part really) wont give in.
The first 4 functions work fine (2 arguments each, a birthday (bday) and a random date (today)). They determine whether or not the random date is a birthday/unbirthday/hundredday/sameweekday, returning True or False if so or not, respectively. First line of every function is the following, rest of the script doesn't matter much.
def birthday(bday, today):
def unbirthday(bday, today):
def hundredday(bday, today):
def sameweekday(bday, today):
Again, these work fine.
The last function has to return all dates, between a certain start and end date, on which one of the above variations of birthdays match. First argument is again bday, the next is start (by default bday, this is the asshole), third is end (by default today) and the fourth is birthday (by default the actual birthday).
def birthdays(bday, start=bday, end=date.today(), birthday=birthday):
its the start=bday that won't work, stating that this bday is undefined. Rest of the script doesn't matter as I don't get so far.
(I import datetime in the beginning of script and all first functions work fine using its tools)
Upvotes: 0
Views: 243
Reputation: 2647
One solution would be to default start=None, and then in the function body have:
if start is None:
start = bday
which should give you the behaviour you want.
Upvotes: 2
Reputation: 360602
You cannot read from a variable before it's been created:
def birthdays(bday, start=bday, end=date.today(), birthday=birthday):
^---1 ^---2
Function arguments as above are just variable name definitions. "The chunk of data inserted into the function call at this point will be named bday
". They don't exist as readable variable WITHIN the function signature, only within the function's body itself. So your #2 above is trying to read from a variable which doesn't exist (yet).
Upvotes: 2