Hello everyone,

i currently trying to write a program that will locate a hyphen in a date in the format YYYY-MM-DD this is what i have so far

date = str(raw_input("Enter a date in YYYY-MM-DD format: "))
i = 0

while i < len(date):
    if date[i] != "-":
        i = i + 1
    print date

but all it does is repeat itself and im not even sure if i wrote the program correctly. (by the end of this what i hope to do is add 21 days to whatever the input date is)
ie 2011-03-03 outputs 2011-03-24

hoping for some help, thanks a bunch

Why do you need to locate the hyphen since the date has a fixed format ?

>>> date = "2011-03-03"
>>> date[4]
'-'
>>> date[7]
'-'

You could also split the date on hyphens to get a list

>>> items = date.split("-")
>>> items
['2011', '03', '03']

Edited 5 Years Ago by Gribouillis: n/a

Comments
thank you !

the program we are asked to write is to say when a library book is due

1. input ask user for date
2. ouput add three weeks to the input

we are suppose use a for or while loop to locate the hyphens and make sure that there is only 2 but im not too sure how to do that..

but all your guys input help out alot

date = input('Input date in format YYYY-MM-DD')

hypCount = date.count('-')

easy enough. No reason for a while loop

Comments
THANKS !

1. input ask user for date
2. ouput add three weeks to the input

Look at the datetime module for this.

>>> import datetime
>>> now = datetime.date(2011, 3, 5)
>>> difference1 = datetime.timedelta(days=21)
>>> print "3 weeks in the future is:", now + difference1
3 weeks in the future is: 2011-03-26
>>>

Edited 5 Years Ago by snippsat: n/a

thank you everyone for all your wonderful input, but unfortunately im not allow to use any of the built infunctions listed above im only allowed to use for loops, if/else/elif statements. but none the less thank you!

also 1 more questions, since i have to do this the long handed way how might i separate the different months
ive tried the following
* ive also tried putting commas but that does work either
if month == "01" or "03" or "05" ..... etc
print "31 days"
elif month == "04" or "06" or ...
print "30 days"
elif month == "02"
print "feb"

is there a way to group it like that ?

Edited 5 Years Ago by xxhellothere: n/a

thank you everyone for all your wonderful input, but unfortunately im not allow to use any of the built infunctions listed above im only allowed to use for loops, if/else/elif statements. but none the less thank you!

also 1 more questions, since i have to do this the long handed way how might i separate the different months
ive tried the following
* ive also tried putting commas but that does work either
if month == "01" or "03" or "05" ..... etc
print "31 days"
elif month == "04" or "06" or ...
print "30 days"
elif month == "02"
print "feb"

is there a way to group it like that ?

You can use

if month in ("04", "06", "09", "11"):
    etc...
elif month == "02":
    etc...
else:
    ...

Edited 5 Years Ago by Gribouillis: n/a

String has count method, but it is enough to split from hyphens and check the result length is three. You have still plenty of places for loops if int(day)+21>28. Assuming using datetime module is not allowed.

Remember to mark your thread solved when you found your answer.

Easier to just make list of each months length and do look up from there, remember that indexes start from zero.

print(["/".join(date.split("-"))])

2011/03/03

from my htc phone ;)

I'd rather do

date.replace('-','/')
This article has been dead for over six months. Start a new discussion instead.