User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,530 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,779 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 1501 | Replies: 11
Reply
Join Date: Oct 2007
Posts: 13
Reputation: Eclipse77 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Eclipse77 Eclipse77 is offline Offline
Newbie Poster

noob in need of guidance for calendar (days of month)

  #1  
Oct 5th, 2007
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. =)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Posts: 268
Reputation: BearofNH is on a distinguished road 
Rep Power: 2
Solved Threads: 19
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz in Training

Tutorial Re: noob in need of guidance for calendar (days of month)

  #2  
Oct 5th, 2007
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:
  1. import datetime
  2. d = datetime.date(2007,10,5)
  3. 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.
Reply With Quote  
Join Date: Oct 2004
Posts: 2,529
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: noob in need of guidance for calendar (days of month)

  #3  
Oct 5th, 2007
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 ...
  1. # use module time to find the day of the year given a date
  2.  
  3. import time
  4.  
  5. #date = raw_input("Enter a date in the form YYYY/MM/DD:")
  6.  
  7. # for testing pick a fixed test date
  8. date = "2007/07/04"
  9.  
  10. # use strptime(date_string, format_string) to form a time tuple ...
  11. time_tuple = time.strptime(date, "%Y/%m/%d")
  12. # test, the time tuple has the following items ...
  13. # (year,month,day,hour,min,sec,weekday(Monday=0),yearday,dls-flag)
  14. # item at index 7 (indexing starts with zero) is the day of the year
  15. print time_tuple
  16. print "%s is day %d of the year" % (date, int(time_tuple[7]))
  17. """
  18. my output -->
  19. (2007, 7, 4, 0, 0, 0, 2, 185, -1)
  20. 2007/07/04 is day 185 of the year
  21. """
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 ...
  1. # use slicing to extract year, month and day from a date string
  2.  
  3. #date = raw_input("Enter a date in the form YYYY/MM/DD:")
  4.  
  5. # for testing pick a fixed test date
  6. date = "2007/09/05"
  7.  
  8. # slice the string and convert to integer values
  9. year = int(date[0:4])
  10. month = int(date[5:7])
  11. day = int(date[8:10])
  12. # test ...
  13. print year, month, day
  14.  
  15. # days in each month, jan is element at index 1 (second element)
  16. # the list has zero as the first element to make calculations easier
  17. # feb has 28 days so this is not a leap year!
  18. month_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  19. # use slicing of the month_day list to create a sub list of days up to
  20. # (but not including) the given month
  21. day_list = month_days[0: month]
  22. # test ...
  23. print day_list
  24.  
  25. year_day = sum(day_list) + day
  26. print date, "is day", year_day, "of the year", year
  27. """
  28. my output -->
  29. 2007 9 5
  30. [0, 31, 28, 31, 30, 31, 30, 31, 31]
  31. 2007/09/05 is day 248 of the year 2007
  32. """
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.
Last edited by vegaseat : Oct 5th, 2007 at 9:30 pm. Reason: code tags
May 'the Google' be with you!
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: noob in need of guidance for calendar (days of month)

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

Jeff
Reply With Quote  
Join Date: Jul 2005
Location: France
Posts: 1,051
Reputation: bumsfeld is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 46
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Veteran Poster

Re: noob in need of guidance for calendar (days of month)

  #5  
Oct 6th, 2007
Jeff,
if you want to know how he did it, just click temporarily on "REPLY W/QUOTE" and look at the text shown.
Should you find Irony, you can keep her!
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: noob in need of guidance for calendar (days of month)

  #6  
Oct 6th, 2007
Well, how about that!

Thanks,
Jeff
Reply With Quote  
Join Date: Jul 2005
Location: France
Posts: 1,051
Reputation: bumsfeld is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 46
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Veteran Poster

Re: noob in need of guidance for calendar (days of month)

  #7  
Oct 6th, 2007
Exciting world we have! One always learns something new!
Should you find Irony, you can keep her!
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: noob in need of guidance for calendar (days of month)

  #8  
Oct 6th, 2007
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.
Reply With Quote  
Join Date: Oct 2004
Posts: 2,529
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: noob in need of guidance for calendar (days of month)

  #9  
Oct 6th, 2007
Originally Posted by jrcagle View Post
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) ...
  1. def isLeapYear(year):
  2. """returns True if the year is a leap year"""
  3. return bool(not year%4 and year%100 or not year%400)
May 'the Google' be with you!
Reply With Quote  
Join Date: Oct 2007
Posts: 13
Reputation: Eclipse77 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Eclipse77 Eclipse77 is offline Offline
Newbie Poster

Re: noob in need of guidance for calendar (days of month)

  #10  
Oct 9th, 2007
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.

  1. [inlinecode]
  2. if (month > 12) or (month < 1):
  3. print "Invalid date."
  4. print "Terminate program and enter the date again in the format requested."
  5. [/inlinecode]


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!
Last edited by Eclipse77 : Oct 9th, 2007 at 12:49 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 4:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC