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']
commented: thank you ! +0
print(["/".join(date.split("-"))])

2011/03/03

from my htc phone ;)

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

commented: THANKS ! +0

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
>>>

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 ?

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:
    ...

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.

To solve your original issue, put the input inside of the while loop.

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

2011/03/03

from my htc phone ;)

I'd rather do

date.replace('-','/')
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.