Re: Nested for loops and print statements
On 09/27/2016 09:20 PM, Steven D'Aprano wrote: On Wednesday 28 September 2016 12:48, Larry Hudson wrote: As they came through in the newsgroup, BOTH run correctly, because both versions had leading spaces only. (I did a careful copy/paste to check this.) Copying and pasting from the news client may not be sufficient to show what whitespace is actually used... Exactly. That's why I pointed out the sometime (mis)handling of tabs in newsreaders, and said these examples came through in MY reader (Thunderbird) as spaces only, so both examples ran correctly. -- -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On Wednesday 28 September 2016 12:48, Larry Hudson wrote: > As they came through in the newsgroup, BOTH run correctly, because both > versions had leading spaces only. > (I did a careful copy/paste to check this.) Copying and pasting from the news client may not be sufficient to show what whitespace is actually used. For example, I use KNode, the KDE news client on Linux, and for me, KNode converts tabs to spaces. (If this is configurable, I don't know how.) BUT if I choose the following menu command: View > View Source I see the raw, unmodified message as posted by the OP, complete with tabs and spaces. > READ the error messages, they are important! Indeed. And *believe* the error messages. -- Steven git gets easier once you get the basic idea that branches are homeomorphic endofunctors mapping submanifolds of a Hilbert space. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On 09/26/2016 01:57 PM, Cai Gengyang wrote: Ok it works now: for row in range(10): for column in range(10): print("*",end="") but how is it different from --- for row in range(10): for column in range(10): print("*",end="") SyntaxError: inconsistent use of tabs and spaces in indentation Why does the example on top work and the example below doesn't work ? The only difference is that the "print" statement is one space different from each other. Forgive me if i can't explain things clearly over the forum As they came through in the newsgroup, BOTH run correctly, because both versions had leading spaces only. (I did a careful copy/paste to check this.) Tabs are sometimes handled incorrectly/inconsistently in newsgroup postings. But if you read the Traceback error message, it is telling you that you have a mix of tabs and spaces _in your original_. READ the error messages, they are important! -- -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
Cai Gengyang wrote: How are you running the interactive interpreter? Are you using IDLE, or are you running Python in a command window? --- IDLE I don't normally use IDLE on MacOSX, so I had to try it to find out. I think I know what your problem is now. When you type a line into IDLE ending with a colon and press enter, it automatically indents the next line, and it does it using *tab* characters. You can see this by selecting the indentation with the mouse. You'll notice that it highlights in big chunks -- those are tab characters. This means that any extra indentation you add yourself also needs to be done by pressing the tab key, not the space bar, otherwise you risk mixing up tabs and spaces in ways that Python doesn't like. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On Tuesday, September 27, 2016 at 2:14:05 PM UTC+8, Cai Gengyang wrote: > We're trying to help, but we need to know more about the > environment you're using to enter your code. > > What operating system are you using? --- OSX Yosemite Version 10.10.2 > > How are you running the interactive interpreter? Are you > using IDLE, or are you running Python in a command window? --- IDLE > > Are you typing code directly into the interactive interpreter, Typing it > directly into IDLE > or are you typing it into a text editor and then copying > and pasting it into the interpreter? > > If you're using a text editor, which one are you using? --- Not using a text > editor > > What *exactly* are you typing on the keyboard to produce > your indentation? Are you pressing the space bar, the > tab key, or some combination of them? Just the space bar, not the tab > key because it gives jumps in spaces that are too large for my liking. > > > > > > > > > On Tuesday, September 27, 2016 at 1:20:17 PM UTC+8, Gregory Ewing wrote: > > Cai Gengyang wrote: > > > I'll still be asking for help here. Please help out a newbie. > > > > We're trying to help, but we need to know more about the > > environment you're using to enter your code. > > > > What operating system are you using? > > > > How are you running the interactive interpreter? Are you > > using IDLE, or are you running Python in a command window? > > > > Are you typing code directly into the interactive interpreter, > > or are you typing it into a text editor and then copying > > and pasting it into the interpreter? > > > > If you're using a text editor, which one are you using? > > > > What *exactly* are you typing on the keyboard to produce > > your indentation? Are you pressing the space bar, the > > tab key, or some combination of them? > > > > -- > > Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
We're trying to help, but we need to know more about the environment you're using to enter your code. What operating system are you using? --- OSX Yosemite Version 10.10.2 How are you running the interactive interpreter? Are you using IDLE, or are you running Python in a command window? --- IDLE Are you typing code directly into the interactive interpreter, Typing it directly into IDLE or are you typing it into a text editor and then copying and pasting it into the interpreter? If you're using a text editor, which one are you using? --- Not using a text editor What *exactly* are you typing on the keyboard to produce your indentation? Are you pressing the space bar, the tab key, or some combination of them? Just the space bar, not the tab key gives too jumps in spaces that are too large for my liking. On Tuesday, September 27, 2016 at 1:20:17 PM UTC+8, Gregory Ewing wrote: > Cai Gengyang wrote: > > I'll still be asking for help here. Please help out a newbie. > > We're trying to help, but we need to know more about the > environment you're using to enter your code. > > What operating system are you using? > > How are you running the interactive interpreter? Are you > using IDLE, or are you running Python in a command window? > > Are you typing code directly into the interactive interpreter, > or are you typing it into a text editor and then copying > and pasting it into the interpreter? > > If you're using a text editor, which one are you using? > > What *exactly* are you typing on the keyboard to produce > your indentation? Are you pressing the space bar, the > tab key, or some combination of them? > > -- > Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
Cai Gengyang wrote: I'll still be asking for help here. Please help out a newbie. We're trying to help, but we need to know more about the environment you're using to enter your code. What operating system are you using? How are you running the interactive interpreter? Are you using IDLE, or are you running Python in a command window? Are you typing code directly into the interactive interpreter, or are you typing it into a text editor and then copying and pasting it into the interpreter? If you're using a text editor, which one are you using? What *exactly* are you typing on the keyboard to produce your indentation? Are you pressing the space bar, the tab key, or some combination of them? -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On Monday, September 26, 2016 at 9:57:52 PM UTC+1, Cai Gengyang wrote: > Ok it works now: > > >>>for row in range(10): > for column in range(10): >print("*",end="") > > > > > but how is it different from --- > > >>> for row in range(10): >for column in range(10): > print("*",end="") > > SyntaxError: inconsistent use of tabs and spaces in indentation > > Why does the example on top work and the example below doesn't work ? The > only difference is that the "print" statement is one space different from > each other. Forgive me if i can't explain things clearly over the forum > Perhaps you'd be more comfortable on the tutor mailing list https://mail.python.org/mailman/listinfo/tutor Kindest regards. Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
Sure, I just sent in a subscription request to it ... but I'll still be asking for help here. Please help out a newbie. When I master this language I can help other new users too (This is good for the world and for everyone involved). Ideally, Information and education should be free and not locked up by a few academics and institutions. On Tuesday, September 27, 2016 at 6:00:40 AM UTC+8, bream...@gmail.com wrote: > On Monday, September 26, 2016 at 9:57:52 PM UTC+1, Cai Gengyang wrote: > > Ok it works now: > > > > >>>for row in range(10): > > for column in range(10): > >print("*",end="") > > > > > > > > > > but how is it different from --- > > > > >>> for row in range(10): > >for column in range(10): > > print("*",end="") > > > > SyntaxError: inconsistent use of tabs and spaces in indentation > > > > Why does the example on top work and the example below doesn't work ? The > > only difference is that the "print" statement is one space different from > > each other. Forgive me if i can't explain things clearly over the forum > > > > Perhaps you'd be more comfortable on the tutor mailing list > https://mail.python.org/mailman/listinfo/tutor > > Kindest regards. > > Mark Lawrence. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
Cai Gengyang : > What is a tab and what is a space in python and what's the difference > ? > > Which piece of code is indented with tabs and which one is indented > with spaces ? Key questions that Python gurus are having a hard time answering! Equally confusing, you might run into this phantom phenomenon: >>> if True: ... a = 3 File "", line 2 a = 3 ^ SyntaxError: invalid character in identifier It might be useful to have a mode that echoes each input line as seen by Python (repr). Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
Ok it works now: >>>for row in range(10): for column in range(10): print("*",end="") but how is it different from --- >>> for row in range(10): for column in range(10): print("*",end="") SyntaxError: inconsistent use of tabs and spaces in indentation Why does the example on top work and the example below doesn't work ? The only difference is that the "print" statement is one space different from each other. Forgive me if i can't explain things clearly over the forum On Tuesday, September 27, 2016 at 3:57:18 AM UTC+8, Larry Hudson wrote: > On 09/26/2016 08:25 AM, Cai Gengyang wrote: > > I just wanted to note that sometimes the code works, sometimes it doesn't. > > (even though both are exactly the same code) ... Weird , dum dum dum > > > > It is NOT weird. Python is being consistent, YOU are not. > > These examples are NOT "exactly the same code"! The indenting is different. > Python (correctly) > treats them as being different. > > YOU MUST USE CONSISTENT INDENTING. You MUST always use spaces (the > recommended) or always use > tabs. Never, ever, not at any time, can you mix them. > > (Now, does that emphasize the point enough?) Go back and REWRITE your code > with CONSISTENT > indenting. > > -- > -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On 9/26/2016 12:54 PM, Cai Gengyang wrote: Which piece of code is indented with tabs and which one is indented with spaces ? I told you in my initial answer, where I said, referring to the two indented lines in one 'piece of code', "These indents are 4 spaces and 1 tabs." It is the mixture in one piece of code that is the problem. The tab in your original post has since be converted to (4) spaces. Tabs do not survive in email, sometimes just being deleted. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On 09/26/2016 08:25 AM, Cai Gengyang wrote: I just wanted to note that sometimes the code works, sometimes it doesn't. (even though both are exactly the same code) ... Weird , dum dum dum It is NOT weird. Python is being consistent, YOU are not. These examples are NOT "exactly the same code"! The indenting is different. Python (correctly) treats them as being different. YOU MUST USE CONSISTENT INDENTING. You MUST always use spaces (the recommended) or always use tabs. Never, ever, not at any time, can you mix them. (Now, does that emphasize the point enough?) Go back and REWRITE your code with CONSISTENT indenting. -- -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
Cai Gengyang writes: > What is a tab and what is a space in python and what's the difference > ? Try print('x\tx') in Python to see a tab character between the two x's. For me it looks the same as seven spaces, for you it will also look like some amount of whitespace but it might be a different amount. The two-character sequence \t inside a Python string literal means tab, same as \x09, same as \u0009, same as \N{TAB}, same as \N{HT}. (These sequences need to be inside quotes.) With print('xx\tx'), the tab looks, for me, the same as six spaces. It's funny that way: it's a single character, but it's shown differently depending on it's position on the line. You may get a tab character by pressing the tab key, but it's usual for that key to be smart in some way or used for some other purpose. > Which piece of code is indented with tabs and which one is indented > with spaces ? I've replaced the tabs with # below, and the greater-than signs with @. This may not have been exactly your code any more, but try to imagine a tab in place of each # anyway. for row in range(10): @for column in range(10): @ # print("*",end="") @ # @ SyntaxError: inconsistent use of tabs and spaces in indentation for row in range(10): @ #for column in range(10): @ # print("*",end="") @ It would be better for you to get a better editor that either prevented you from getting into this mess in the first place, or at least showed where the offending characters are. It may be possible to configure your editor to be better. The details depend on what exactly you are using. What are you using? -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On 09/26/2016 06:54 PM, Cai Gengyang wrote: What is a tab and what is a space in python and what's the difference ? Which piece of code is indented with tabs and which one is indented with spaces ? Please do not top-post in this list. Put your text after the message you quote. Tabs and spaces are 2 different characters, tab ASCII code is 9, space ASCII code is 32. You *need* to understand that they are 2 differents characters. The problem come with their representation. 1/ a space is represented by 1 whitespace 2/ a tab is represented by x whitespaces. The number x depends on your text editor. When you want to indent a python block, you put whitespaces on its left to indent it. You can indent using either tabs or spaces. Python does not tell you which one to use, but it tells you to choose *one* method, and stick with it. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On Tue, 27 Sep 2016 01:25 am, Cai Gengyang wrote: > I just wanted to note that sometimes the code works, sometimes it doesn't. > (even though both are exactly the same code) ... Weird , dum dum dum They are not the same code. One of them mixes tabs and spaces for the same indent level, the other does not. Here is a hint how you can tell the difference between lines with tabs and lines with spaces in the Python interactive interpreter: - if you pressed the spacebar repeatedly to get the indent, like SPACEBAR SPACEBAR SPACEBAR SPACEBAR, then the line will be indented with spaces; - if you pressed the tab key to get the indent, like TAB TAB, then the line will be indented with tabs; - if you pressed the tab key first, then spaces, like TAB SPACEBAR, then the line will be indented with a mixture of tabs and spaces. If you are copying code from a text editor, check your editor's settings. Some editors will be configured to insert spaces when you press the tab key. Your first example has: 1st line: no indent 2nd line: seven spaces 3rd line: tab + two spaces Notice that the 2nd and 3rd line start with inconsistent indentation: one uses spaces, the other uses tab. This is bad. Your second example has: 1st line: no indent 2nd line: tab 3rd line: tab + three spaces Notice that the 2nd and 3rd line start with the same indentation: both start with a tab. This is better, it is enough to satisfy the interpreter, but it would be better if you picked one (spaces) or the other (tabs) and ONLY used that. Other wise you will just confuse yourself. But you won't confuse the interpreter. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
What is a tab and what is a space in python and what's the difference ? Which piece of code is indented with tabs and which one is indented with spaces ? On Tuesday, September 27, 2016 at 12:40:16 AM UTC+8, MRAB wrote: > On 2016-09-26 16:25, Cai Gengyang wrote: > > I just wanted to note that sometimes the code works, sometimes it doesn't. > > (even though both are exactly the same code) ... Weird , dum dum dum > > > for row in range(10): > >for column in range(10): > > print("*",end="") > > > > SyntaxError: inconsistent use of tabs and spaces in indentation > for row in range(10): > > for column in range(10): > >print("*",end="") > > > [snip] > > They are not exactly the same code. They are indented differently (tabs > vs spaces), and that matters in Python. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On 2016-09-26 16:25, Cai Gengyang wrote: I just wanted to note that sometimes the code works, sometimes it doesn't. (even though both are exactly the same code) ... Weird , dum dum dum for row in range(10): for column in range(10): print("*",end="") SyntaxError: inconsistent use of tabs and spaces in indentation for row in range(10): for column in range(10): print("*",end="") [snip] They are not exactly the same code. They are indented differently (tabs vs spaces), and that matters in Python. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On 09/26/2016 05:25 PM, Cai Gengyang wrote: I just wanted to note that sometimes the code works, sometimes it doesn't. (even though both are exactly the same code) ... Weird , dum dum dum for row in range(10): for column in range(10): print("*",end="") SyntaxError: inconsistent use of tabs and spaces in indentation for row in range(10): for column in range(10): print("*",end="") Here, from itertools import product for row,column in product(range(10), range(10)): print("*", end="") Problem solved :) On a more serious note, they're not the same code, in the same way than print("foo") print("bar") is not the same code than print("foo")print("bar") You're fooled by your editor which displays 2 different characters the same way. Tab or space does matter in python, get over it and move on with your python life. Actually python is a great debugger for misconfigured text editors. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
I just wanted to note that sometimes the code works, sometimes it doesn't. (even though both are exactly the same code) ... Weird , dum dum dum >>> for row in range(10): for column in range(10): print("*",end="") SyntaxError: inconsistent use of tabs and spaces in indentation >>> for row in range(10): for column in range(10): print("*",end="") On Monday, September 26, 2016 at 5:36:26 PM UTC+8, Steven D'Aprano wrote: > On Monday 26 September 2016 18:32, Cai Gengyang wrote: > > > These are my attempts --- > > That's nice. Do you have a question? > > > > SyntaxError: inconsistent use of tabs and spaces in indentation > > When you indent, press TAB or SPACE but not both. > > This error can only happen if you are use spaces for some lines and tabs for > other lines. Do not do that. Use spaces only or tabs only, never both. > > > > SyntaxError: expected an indented block > > You forgot to indent at all. You must indent with at least one space or one > tab. > > > SyntaxError: inconsistent use of tabs and spaces in indentation > > Use only spaces, or only tabs, never both. If you use spaces for one line, > then > always use spaces. If you use tabs for one line, then always use tabs. > > > SyntaxError: inconsistent use of tabs and spaces in indentation > > Same as above. > > > > > > -- > Steven > git gets easier once you get the basic idea that branches are homeomorphic > endofunctors mapping submanifolds of a Hilbert space. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On Monday 26 September 2016 18:32, Cai Gengyang wrote: > These are my attempts --- That's nice. Do you have a question? > SyntaxError: inconsistent use of tabs and spaces in indentation When you indent, press TAB or SPACE but not both. This error can only happen if you are use spaces for some lines and tabs for other lines. Do not do that. Use spaces only or tabs only, never both. > SyntaxError: expected an indented block You forgot to indent at all. You must indent with at least one space or one tab. > SyntaxError: inconsistent use of tabs and spaces in indentation Use only spaces, or only tabs, never both. If you use spaces for one line, then always use spaces. If you use tabs for one line, then always use tabs. > SyntaxError: inconsistent use of tabs and spaces in indentation Same as above. -- Steven git gets easier once you get the basic idea that branches are homeomorphic endofunctors mapping submanifolds of a Hilbert space. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
Cai Gengyang writes: > These are my attempts --- > for row in range(10): > for column in range(10): > print("*",end=" ") > > SyntaxError: inconsistent use of tabs and spaces in indentation What do you type yourself? Could it be that your software starts the second line with a tab automatically and then you type spaces yourself? When I type commands to the Python interpreter, it looks like this: >>> if 1: ...print(1) ... 1 The interpreter inserts the dots below the prompt, and a space, and here I have inserted three more spaces for indentation. The interpreter then ignores those dots and space, and it doesn't seem to let me use any tabs anyway. You are running the interpreter in a different way. That's fine. But what exactly are you doing? (If you are using the spacebar, try the tabulation key instead. If you are using the tabulation key, try the spacebar instead.) -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
Steven D'Aprano writes: > P.S. Hey Jussi, is the backspace key on your keyboard broken? Every > time somebody bottom-posts without trimming, a pixie dies... I was annoyed by the top-posted one-liner in response to the last line of Terry's response. I responded in kind and then it was too late. I'm sorry. It was a childish thing to do. Backspace key is fine. I tend to use C-w and C-k for trimming, and they are fine, too. I was the broken part. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
These are my attempts --- >>> for row in range(10): for column in range(10): print("*",end=" ") SyntaxError: inconsistent use of tabs and spaces in indentation >>> for row in range(10): for column in range(10): print("*",end=" ") SyntaxError: expected an indented block >>> for row in range(10): for column in range(10): print("*",end=" ") SyntaxError: inconsistent use of tabs and spaces in indentation >>> for row in range(10): for column in range(10): print("*",end=" ") SyntaxError: inconsistent use of tabs and spaces in indentation >>> for row in range(10): for column in range(10): print("*",end=" ") * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * On Monday, September 26, 2016 at 4:16:37 PM UTC+8, Steven D'Aprano wrote: > On Monday 26 September 2016 17:21, Jussi Piitulainen wrote: > > > Cai Gengyang writes: > [snip 80 or so lines] > > Reindent your lines. > > In case Cai doesn't know what "reindent" means: > > > It depends on your text editor. At worst, you have to delete all the indents, > and re-enter them, using ONLY spaces, or ONLY tabs, but never mixing them. > > Some text editors may have a command to reindent, or clean indentation, or > fix > indentation, or something similar. > > Or you can use the tabnanny.py program: > > > python -m tabnanny path/to/file.py > > > > P.S. Hey Jussi, is the backspace key on your keyboard broken? Every time > somebody bottom-posts without trimming, a pixie dies... > > > > -- > Steven > git gets easier once you get the basic idea that branches are homeomorphic > endofunctors mapping submanifolds of a Hilbert space. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On Monday 26 September 2016 17:21, Jussi Piitulainen wrote: > Cai Gengyang writes: [snip 80 or so lines] > Reindent your lines. In case Cai doesn't know what "reindent" means: It depends on your text editor. At worst, you have to delete all the indents, and re-enter them, using ONLY spaces, or ONLY tabs, but never mixing them. Some text editors may have a command to reindent, or clean indentation, or fix indentation, or something similar. Or you can use the tabnanny.py program: python -m tabnanny path/to/file.py P.S. Hey Jussi, is the backspace key on your keyboard broken? Every time somebody bottom-posts without trimming, a pixie dies... -- Steven git gets easier once you get the basic idea that branches are homeomorphic endofunctors mapping submanifolds of a Hilbert space. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
Cai Gengyang writes: > 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 Reindent your lines. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
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. -- https://mail.python.org/mailman/listinfo/python-list
Nested for loops and print statements
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() * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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=" ") SyntaxError: inconsistent use of tabs and spaces in indentation -- https://mail.python.org/mailman/listinfo/python-list