Nested for loops and print statements

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Mon Sep 26 02:29:09 EDT 2016


Cai Gengyang writes:

> Why is it that you need a print() at the end to create the table for
> example 1:
>
> Example 1 ---
>
>>>> for row in range(10):
>     for column in range(10):
>         print("*",end=" ")
>  
>     # Print a blank line for next row
>     print()

[- -]

> When I try to do example 1 without the print() statement at the end, I
> get this error :
>
> for row in range(10):
>     for column in range(10):
> 	print("*",end=" ")
> 	
> SyntaxError: inconsistent use of tabs and spaces in indentation

That error has absolutely nothing to do with nested loops, and
absolutely nothing to do with print statements. Nested loops and print
statements are entirely coincidental to the error.

You have managed to use a mixture of two different whitespace characters
to indent the lines: horizontal tabs (character code 9, hex 9) and
spaces (character code 32, hex 20). Different environments may interpret
and display the tabs as different amounts of indentation, depending on
their position in the line. That affects the meaning of a Python
program.

When you get this error, re-indent your lines.



More information about the Python-list mailing list