Hi everyone, I'm new here and I'm looking forward to seeing how everyone codes in python. I'm a complete newbie, who has never done any programming in his whole life (except for the past 2 week). I read the guidelines about asking for help on assignments, and I wouldn't have posted here unless I was completely stuck. I'm hoping someone here can give me some tips or get me started. I basically have to create a program that tells me how many days have passed in the date the user input. Here's an example:

Enter a date in the form YYYY/MM/DD: 2000/01/01
2000/01/01 is day 1 of the year 2000.

Here is another example:
Enter a date in the form YYYY/MM/DD: 2007/09/05
2007/09/05 is day 248 of the year 2007.

I'll have to worry about leap years later, but I want to get the basics first. I'm so jumbled and confused on what codes I should use. So far, we were only taught the very basics of, "print/raw_input/if/elif/else/while & for loops/random/variables"

We were told to use string indexing but i'm not sure how I should use it because we weren't taught during class.

Here's what I have so far....if anyone can shed some light, it would be soooo much appreciated. And again, I'm in a Psychology Major and I have zero experience with coding which makes this so frustrating.
_______________________________________________________________
print "Enter a date in the form YYYY/MM/DD:"
date = int(raw_input("Enter a date in the form YYYY/MM/DD:"))
if date == "":
print "YYYY/MM/DD"

#the number of days at the start of each month
jan=1
feb=32
mar=60
apr=91
may=121
jun=152
jul=182
aug=213
sep=244
oct=274
nov=305
dec=335

print date[26:30] + "/" + date[31:33] + "/" + date[34:36]


date = int(raw_input("Enter a date in the form: YYYY/MM/DD:"))
print date[41:45],date[46:48],date[49:51], "is day", "???", "of the year."
________________________________________________________________

I know that I have everything everywhere, but I guess I need a starting point. I'm so lost. Thanks everyone in advance!

p.s. - I finished the program of guessing a random number the computer generated and it feels so good to complete a program. =)

Thanks for reading the guidelines.

Most of what you need is in the Python date library. Take a look at http://docs.python.org/lib/datetime-date.html for starters. You'd want to do something like:

import datetime
d = datetime.date(2007,10,5)
print d.weekday()

Note the arguments to datetime.date() are integers, not strings. (Hint: there's an int() function to convert strings to integers).

Please learn to use the code tags in your posts. I'd give a better example but don't know how to display the tags without entering code mode. See the
Announcement: Please use BB Code and Inlinecode tags on the Python forum main page.

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.

OK Vega, fess up: how do you get the bolded [ code = Python ] text in there?! I tried several kludges and failed.

Jeff

Jeff,
if you want to know how he did it, just click temporarily on "REPLY W/QUOTE" and look at the text shown.

Well, how about that!

Thanks,
Jeff

Exciting world we have! One always learns something new!

Last year, I went a day without my watch. At one point, I asked a student what time it was. She replied, "Look at your cell phone!"

Duh.

Last year, I went a day without my watch. At one point, I asked a student what time it was. She replied, "Look at your cell phone!"

Duh.

The same thing just happened to me! I guess I am a member of the older generation!

If you want to know whether a year is a leap year, use this little function (from ffao) ...

def isLeapYear(year):
    """returns True if the year is a leap year"""
    return bool(not year%4 and year%100 or not year%400)

i think asking help here should be allowed for assignments right? it's the same as asking a prof for help correct? just in case they know i'm posting here, it's not considered cheating since i'm still writing my own code, with help..that's all. anyways, how do i fix my output when i input a month <0 and month >12. here's my code.

if (month > 12) or (month < 1):
    print "Invalid date."
    print "Terminate program and enter the date again in the format requested."    

After the last print line, i want the program to stop. what's the code for that? As in, i need to run the program again and input the correct month.
Thanks!

You don't have to exit the program, just stay in a loop until conditions are met ...

while True:
    date = raw_input("Enter a date in the form YYYY/MM/DD:")
    # slice the string and convert to integer values
    year = int(date[0:4])
    month = int(date[5:7])
    day = int(date[8:10])
    if month in range(1, 13):
        # ok, break out of the while loop
        break
    else:
        # stay in the while loop
        print "month should be between 01 and 12"
 
# test
print year, month, day

Things get hairy with correct days.

If you want to exit a program, this line of code will do ...

raise SystemExit

Validating input is one of my big themes with my students. It's usually accomplished like this:

(note no 'inlinecode' tags)

while True:
   inp = raw_input("prompt: ")
   if inp_is_valid:
      process_inp
      break
   else:
      print "Helpful Error Message"
...

That's the usual framework, then you can fill in the details.

Once you've learned functions, you can throw all the validation into a function.

Hope it helps,
Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.