Welcome to DaniWeb!
Python's syntax might be easy for a beginner, but you have to remember that Python's power comes from knowing which of the many modules to use to make coding easier. In your case the module time will save you a lot of time (no pun intended). With it you can create a collection of time elements called a time tuple that amongst other things contains the day of the year of a given date ...
# use module time to find the day of the year given a date
import time
#date = raw_input("Enter a date in the form YYYY/MM/DD:")
# for testing pick a fixed test date
date = "2007/07/04"
# use strptime(date_string, format_string) to form a time tuple ...
time_tuple = time.strptime(date, "%Y/%m/%d")
# test, the time tuple has the following items ...
# (year,month,day,hour,min,sec,weekday(Monday=0),yearday,dls-flag)
# item at index 7 (indexing starts with zero) is the day of the year
print time_tuple
print "%s is day %d of the year" % (date, int(time_tuple[7]))
"""
my output -->
(2007, 7, 4, 0, 0, 0, 2, 185, -1)
2007/07/04 is day 185 of the year
"""
Module time will also take care of leap years and flag down impossible dates.
If you are not allowed to use the time module yet, and want to make this an exercise in slicing of strings and lists, you can do the following ...
# use slicing to extract year, month and day from a date string
#date = raw_input("Enter a date in the form YYYY/MM/DD:")
# for testing pick a fixed test date
date = "2007/09/05"
# slice the string and convert to integer values
year = int(date[0:4])
month = int(date[5:7])
day = int(date[8:10])
# test ...
print year, month, day
# days in each month, jan is element at index 1 (second element)
# the list has zero as the first element to make calculations easier
# feb has 28 days so this is not a leap year!
month_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# use slicing of the month_day list to create a sub list of days up to
# (but not including) the given month
day_list = month_days[0: month]
# test ...
print day_list
year_day = sum(day_list) + day
print date, "is day", year_day, "of the year", year
"""
my output -->
2007 9 5
[0, 31, 28, 31, 30, 31, 30, 31, 31]
2007/09/05 is day 248 of the year 2007
"""
I recommend you really get familiar with slicing, a power horse within Python. Study the code, if you have any questions, ask the forum members, we will gladly explain. Don't be shy, we all had to start at some time.
Note, for Python on Daniweb:
Please use the[code=python] and [/code] tag pair to enclose your python code. This way the important code indentations are correctly shown.
If you want to copy the code into your editor, click on the "Toggle Plain Text" option to remove the line numbers.