•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 13
Reputation:
Rep Power: 2
Solved Threads: 0
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. =)
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:
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.
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:
Python Syntax (Toggle Plain Text)
import datetime d = datetime.date(2007,10,5) print d.weekday()
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 ...
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 ...
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.
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 ...
python Syntax (Toggle Plain Text)
# 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 """
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 ...
python Syntax (Toggle Plain Text)
# 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 """
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!
•
•
•
•
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.
If you want to know whether a year is a leap year, use this little function (from ffao) ...
python Syntax (Toggle Plain Text)
def isLeapYear(year): """returns True if the year is a leap year""" return bool(not year%4 and year%100 or not year%400)
May 'the Google' be with you!
•
•
Join Date: Oct 2007
Posts: 13
Reputation:
Rep Power: 2
Solved Threads: 0
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.
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!
python Syntax (Toggle Plain Text)
[inlinecode] if (month > 12) or (month < 1): print "Invalid date." print "Terminate program and enter the date again in the format requested." [/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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Help with Creating a Yearly Calendar (C++)
- validate days in a month (Pascal and Delphi)
- Pop-Up Calendar for ASP (ASP)
- calendar problem again (C)
- calendar problem (C)
- calendar how mny days (C)
- how many days in each month? (Software Developers' Lounge)
Other Threads in the Python Forum
- Previous Thread: Ping in a loop
- Next Thread: initalze class by a condition



Linear Mode