0

I have a code that can tell the weather in entered location. I want to make an option to print the weather for the next 3 days ,I need to send to my function 3 dates (with a loop), every time different date, how can I send dates of next 3 days from current day?

#This is my function
def weather(city, date): 

#This is the part where I send it from the main to the function:
city = 'Paris'
while(i < 4):
    i += 1
    weather(city.lower(), dd/mm/yyyy)# Here instead of "dd/mm/yyyy" I need to send every time the next date from today.
vic
  • 11
  • 1

2 Answers2

0

Use datetime.timedelta(days=1) to increment your day by 1 as follows, you can pass this date to your function

import datetime
curr_date = datetime.datetime.now()

for i in range(4):
    curr_date += datetime.timedelta(days=1)
    print(curr_date)
#2019-04-19 22:01:29.503352
#2019-04-20 22:01:29.503352
#2019-04-21 22:01:29.503352
#2019-04-22 22:01:29.503352
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
0

The simplest way to generate a date range is to use pandas.date_range as

import pandas as pd

dates = pd.date_range('2019-04-10', periods=3, freq='D')

for day in dates:
    weather(city, day)

Or you may insist on a loop for next days, you can use datetime.timedelta

from datetime import date, timedelta

one_day_delta = timedelta(1)
day = datetime.date(2019, 4, 19)
for i in range(3)
    day += one_day_delta
    weather(city, day)
David Wu
  • 193
  • 2
  • 10
  • In your second example, if I dont know when the user wiil use the code, I need it to know the date without me typing "day = datetime.date(2019, 4, 19)" and I also need it in this format: dd/mm/yyyy – vic Apr 18 '19 at 16:54
  • Just use datetime.strptime to parse datetime string and take the date part. ```python day = datetime.strptime('01/04/2019', '%d/%m/%Y').date() ``` And you can get the date object. @vic – David Wu Apr 18 '19 at 17:13