0

This is my code:

from datetime import datetime, timedelta
from dateutil.relativedelta import *

date = futuredate = datetime(2021, 4, 12, 0, 0)
date = date + relativedelta(days=11) + relativedelta(years=-1)

I'm trying to loop this part of the code: date = date + relativedelta(days=11) + relativedelta(years=-1) so that it will stop to the year the user asked for.

For example the user wanted to see the figure for 2001, the code will stop looping when the year reaches 2001.

Additionally, I want the code to switch to days=-11 and years=1 when the user asks for a year that is above 2021.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
nayim
  • 1
  • What trouble are you having with the loop? – Jon Clements Jun 19 '21 at 16:57
  • I don't know how to loop it. – nayim Jun 19 '21 at 16:58
  • 2
    Have you used loops at all? – Jon Clements Jun 19 '21 at 17:00
  • Not really, I'm kind of new to python and I'm trying out a hard question from school to up my knowledge. – nayim Jun 19 '21 at 17:02
  • Did you read this already? https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming – mkrieger1 Jun 19 '21 at 17:03
  • That specifically, No. But I've read the concept of loops but in this piece of code with the block of dates is what I can't get a grip on. – nayim Jun 19 '21 at 17:05
  • If the user queries year 2001, why don't you *set* that year; why do you need a loop at all? I mean, what problem are you *actually* try to solve? – FObersteiner Jun 19 '21 at 17:58
  • That was an example, They would input any year, What I'm trying to solve is a program that can show when Ramadan was or will be. As years go by, Next year's Ramadan will be pushed forward by 11 days (e.g this year was 12th apr next year will be 1st apr) – nayim Jun 19 '21 at 18:14
  • All I want to solve is how can I loop this line; "date = date + relativedelta(days=11) + relativedelta(years=-1)" so that if someone inputted 2018, it would have to loop until it reaches 2018. Vice versa with the code flipped "date = date + relativedelta(days=-11) + relativedelta(years=1)" when the user inputs a year above 2021. – nayim Jun 19 '21 at 18:17
  • That's a little more complex since the datetime module is based on the Gregorian calendar. But there are third party libraries which help, as e.g. shown [here](https://stackoverflow.com/a/60750520/10197418) – FObersteiner Jun 20 '21 at 10:36
  • My code uses Gregorian calendar to calculate when next Ramadan will be, meaning that my issue is that how can I loop this simple line: a = date + relativedelta(days=11) + relativedelta(years=-1) #this one is for past dates b = date + relativedelta(days=-11) + relativedelta(years=1) #this one is for future dates so that when the computer sees it reaches the year the user inputted, it'll stop. – nayim Jun 20 '21 at 17:09
  • Again: you can do this calculation in one step, no need for a loop. But it will likely be wrong because the Islamic year doesn't always have 11 days less than Gregorian calendar year. – FObersteiner Jun 21 '21 at 05:53

0 Answers0