0

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

7
Contributors
12
Replies
13
Views
5 Years
Discussion Span
Last Post by jice
Featured Replies
  • Why do you need to locate the hyphen since the date has a fixed format ? [code=python] >>> date = "2011-03-03" >>> date[4] '-' >>> date[7] '-' [/code] You could also split the date on hyphens to get a list [code=python] >>> items = date.split("-") >>> items ['2011', '03', '03'] … Read More

  • [QUOTE]1. input ask user for date 2. ouput add three weeks to the input[/QUOTE] Look at the [URL="http://docs.python.org/library/datetime.html"]datetime module[/URL] for this. [CODE]>>> 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 … Read More

1

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 by Gribouillis: n/a

Comments
thank you !
0

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

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

hypCount = date.count('-')

easy enough. No reason for a while loop

Comments
THANKS !
1

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 by snippsat: n/a

0

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 by xxhellothere: n/a

0

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 by Gribouillis: n/a

0

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.

0

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

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

2011/03/03

from my htc phone ;)

I'd rather do

date.replace('-','/')
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.