Nested for loops and print statements
Cai Gengyang
gengyangcai at gmail.com
Mon Sep 26 03:19:03 EDT 2016
So what do i need to do to correct the error ?
Regards
On Monday, September 26, 2016 at 2:48:16 PM UTC+8, Terry Reedy wrote:
> On 9/26/2016 1:59 AM, Cai Gengyang wrote:
> > 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()
>
> These indents are either 4 or 8 spaces.
>
> The print provides a carriage return.
> Each line ends with a space.
>
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
> > * * * * * * * * * *
>
> One can avoid both extra print and spaces with
>
> for row in range(10):
> for column in range(10):
> print("*", end=" " if column<9 else '\n')
>
> # or
> for row in range(10):
> print(' '.join(['*']*10))
> # or
> print((' '.join(['*']*10)+'\n')*10)
>
> # or
> for row in range(10):
> print('* '*9 + '*')
> # or
> print(('* '*9 + '*\n')*10)
>
>
> > but not for Example 2 ---
> >
> > for row in range(10):
> > print("*",end=" ")
> >
> > * * * * * * * * * *
> >
> > 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=" ")
>
> These indents are 4 spaces and 1 tabs.
>
> > SyntaxError: inconsistent use of tabs and spaces in indentation
>
> Because you mixed tabs and spaces. Has nothing to do with print statement.
>
>
> --
> Terry Jan Reedy
More information about the Python-list
mailing list