0

I am very new to python, so please forgive any basic mistakes. I took this block of code from a website (and it looks fairly straightforward) so I thought it would work. Basically, I want this code block to print out all the dates between 2015 and 2016. So far as I can tell, the indentation looks fine. I've tried adjusting the indentations as well, but I haven't had any luck in getting this to work. Any help would be greatly appreciated.

import calendar

cal = calendar.Calendar()

for year in range(2015,2016):
    for month in range(1,13):
        monthdays = [d for d in cal.itermonthdays(year,month) if d != 0]    
        for day in monthdays:
            r = str(year) + str(month) + str(day)
            print(r)

Whenever the code runs, I get this error:

>>> for year in range(2015,2016):
... for month in range(1,13):
File "<stdin>", line 2
    for month in range(1,13):
    ^
IndentationError: expected an indented block
>>> monthdays = [d for d in cal.itermonthdays(year,month) if d != 0]
>>> for day in monthdays:
... r = str(year) + str(month) + str(day)
File "<stdin>", line 2
    r = str(year) + str(month) + str(day)
    ^
IndentationError: expected an indented block

Thanks in advance.

JiggidyJoe
  • 489
  • 2
  • 8
  • 21
  • after a for statement, indention of 4 spaces is required – Chen A. Sep 30 '17 at 08:20
  • Is your code indented exactly the same as in your question? I have a feeling that copy/paste into your editor messed up the indentation but copy/paste into the question here preserved it. – roganjosh Sep 30 '17 at 08:21
  • Are you using spaces only ? Python doesn't like if you mix tabs and spaces to indent – FlorianGD Sep 30 '17 at 08:33
  • I've just typed the code into notepad. No cut and paste. And the same error still occurs. – JiggidyJoe Sep 30 '17 at 08:54

1 Answers1

1

Indentation is important in Python. If you have code C or Java before, a block is between { and }, but in Python it's different, indented lines after : are considered as a "block". Lines after if, while, def, etc must be indented more, either by spaces or tab.

The problem is that you possibly copy-and-paste that code into the IDE or terminal and the indentation is not preserved.

Look at these lines:

   >>> for year in range(2015,2016):
   ... for month in range(1,13):

Both for are on the same indent level, no space/tab before them. It's the problem. It should be:

   >>> for year in range(2015,2016):
   ...     for month in range(1,13):

The indentation can be a space, two spaces, or more, or tabs, as long as they are consistent.

And so on...

fikr4n
  • 3,250
  • 1
  • 25
  • 46
  • I've just taken the code out and placed it into notepad. I then changed the indentation to 2 spaces per indent. The same error occurs. – JiggidyJoe Sep 30 '17 at 08:43
  • I've just typed the code into notepad. No cut and paste. And the same error still occurs. – JiggidyJoe Sep 30 '17 at 08:56
  • @JiggidyJoe are you sure the code is exactly like in your question? If so, please check the consistency of space or tab. Or copy it from your question instead of from the website you originally copied from. – fikr4n Sep 30 '17 at 08:58
  • That is so strange!! Thanks BornToCode. I copied it from the question and pasted it straight into python and it worked. The question is, how come it still didn't work when I manually typed it into notepad? – JiggidyJoe Sep 30 '17 at 09:03
  • @JiggidyJoe Hm... probably you mixed tabs and spaces. – fikr4n Sep 30 '17 at 09:12
  • Okay. I'll have to keep an eye out for those tabs and spaces. Thanks very much for the help. Thanks to everyone for their input and their help. Very much appreciated. Cheers. – JiggidyJoe Sep 30 '17 at 09:23
  • Looks like I got affected by this: https://stackoverflow.com/questions/3366499/notepad-indentation-messes-up – JiggidyJoe Sep 30 '17 at 10:32