[Tutor] Is there a method like this already?
Alan Gauld
alan.gauld at freenet.co.uk
Tue Jul 25 05:46:37 CEST 2006
> Is there a method in Python like this already:
> #This program calculates how many days it has been from one day to
> the other.
Look at the datetime module it has methods for getting differences in
daes/times etc.
> def first_date():
> 1y = int(raw_input("Enter the year of the first date: "))
> 1m = int(raw_input("Enter the month of the first date: "))
> 1d = int(raw_input("Enter the day of the first date: "))
Doesn't return anything so the data is lost as soon as
you exit the function. You need to return the data(as a tuple?)
so you can store it in a variable:
return (1y,1m,1d)
date1 = first_date()
> def second_date():
> 2y = int(raw_input("Enter the year of the second date: "))
etc
And this does exactly the same as first_date so you don't need it.
Rename first_date to get_date(), remove the ones, and just call it
twice:
def get_date():
y = int(raw_input("Enter the year: "))
m = int(raw_input("Enter the month: "))
d = int(raw_input("Enter the day: "))
return (y,m,d)
print "For the first date"
date1 = get_date()
print "For the second date"
date2 = get_date()
> def calculate_days(year, month, day):
And this would dake the two dates as arguments and return
a number of dats:
def calculate_days(firstDate, secondDate):
# use datetime module here....
HTH
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list