Re: While, If, Count Statements
On Tuesday, November 28, 2017 at 8:12:09 PM UTC+8, Frank Millman wrote: > "Cai Gengyang" wrote in message > news:a8335d2c-1fb9-4ba9-b752-418d19e57...@googlegroups.com... > > > > On Tuesday, November 28, 2017 at 4:18:04 PM UTC+8, Frank Millman wrote: > > > "Cai Gengyang" wrote in message > > > news:c2dfc9c4-3e16-480c-aebf-553081775...@googlegroups.com... > > > > > > > Sure, so how would the code look like if I want the "if" statement to > > > > be > > > > nested inside the "while" loop > > > > > > Have you tried putting the 'if' statement inside the 'while' loop? > > > > > > If not, give it a shot and see what happens. > > > > > > Frank Millman > > > > I tried this : > > > > count = 0 > > > > while count < 10: > > if count < 5: > > print "Hello, I am an if statement and count is", count > > print "Hello, I am a while and count is", count > > count += 1 > > > > but it gives an "indentation error: expected an indented block" with an > > arrow pointing at the count after the 3rd statement. Indentation error is > > supposed to be an error about tabs and spaces right ? But I can't find any > > mistakes with it ... > > You are almost there. > > An 'if' statement always requires that the following statements are > indented. This applies even if you are already at one level of indentation. > > You could have, for example - > > if a == 'something': > if b == 'something else': > if c == 'and another one': > do_something_if_a_and_b_and_c_are_true() > > Or in your case - > > while condition: > if a == 'something': > do_something_if_a_is_true() > continue with while clause > > Indentation is fundamental to the way Python works, so if anything above is > not clear, query it now. It is essential that you have a firm grasp of this. > > HTH > > Frank It works now ! All I had to shift the 2nd "print" statement up a few spaces and it worked --- This is my code: count = 0 while count < 10: if count < 5: print "Hello, I am an if statement and count is", count print "Hello, I am a while and count is", count count += 1 Output : Hello, I am an if statement and count is 0 Hello, I am a while and count is 0 Hello, I am an if statement and count is 1 Hello, I am a while and count is 1 Hello, I am an if statement and count is 2 Hello, I am a while and count is 2 Hello, I am an if statement and count is 3 Hello, I am a while and count is 3 Hello, I am an if statement and count is 4 Hello, I am a while and count is 4 Hello, I am a while and count is 5 Hello, I am a while and count is 6 Hello, I am a while and count is 7 Hello, I am a while and count is 8 Hello, I am a while and count is 9 -- https://mail.python.org/mailman/listinfo/python-list
Re: While, If, Count Statements
"Cai Gengyang" wrote in message news:a8335d2c-1fb9-4ba9-b752-418d19e57...@googlegroups.com... On Tuesday, November 28, 2017 at 4:18:04 PM UTC+8, Frank Millman wrote: > "Cai Gengyang" wrote in message > news:c2dfc9c4-3e16-480c-aebf-553081775...@googlegroups.com... > > > Sure, so how would the code look like if I want the "if" statement to > > be > > nested inside the "while" loop > > Have you tried putting the 'if' statement inside the 'while' loop? > > If not, give it a shot and see what happens. > > Frank Millman I tried this : count = 0 while count < 10: if count < 5: print "Hello, I am an if statement and count is", count print "Hello, I am a while and count is", count count += 1 but it gives an "indentation error: expected an indented block" with an arrow pointing at the count after the 3rd statement. Indentation error is supposed to be an error about tabs and spaces right ? But I can't find any mistakes with it ... You are almost there. An 'if' statement always requires that the following statements are indented. This applies even if you are already at one level of indentation. You could have, for example - if a == 'something': if b == 'something else': if c == 'and another one': do_something_if_a_and_b_and_c_are_true() Or in your case - while condition: if a == 'something': do_something_if_a_is_true() continue with while clause Indentation is fundamental to the way Python works, so if anything above is not clear, query it now. It is essential that you have a firm grasp of this. HTH Frank -- https://mail.python.org/mailman/listinfo/python-list
Re: While, If, Count Statements
On Tuesday, November 28, 2017 at 4:18:04 PM UTC+8, Frank Millman wrote: > "Cai Gengyang" wrote in message > news:c2dfc9c4-3e16-480c-aebf-553081775...@googlegroups.com... > > > Sure, so how would the code look like if I want the "if" statement to be > > nested inside the "while" loop > > Have you tried putting the 'if' statement inside the 'while' loop? > > If not, give it a shot and see what happens. > > Frank Millman I tried this : count = 0 while count < 10: if count < 5: print "Hello, I am an if statement and count is", count print "Hello, I am a while and count is", count count += 1 but it gives an "indentation error: expected an indented block" with an arrow pointing at the count after the 3rd statement. Indentation error is supposed to be an error about tabs and spaces right ? But I can't find any mistakes with it ... -- https://mail.python.org/mailman/listinfo/python-list
Re: While, If, Count Statements
"Cai Gengyang" wrote in message news:c2dfc9c4-3e16-480c-aebf-553081775...@googlegroups.com... Sure, so how would the code look like if I want the "if" statement to be nested inside the "while" loop Have you tried putting the 'if' statement inside the 'while' loop? If not, give it a shot and see what happens. Frank Millman -- https://mail.python.org/mailman/listinfo/python-list
Re: While, If, Count Statements
On Tuesday, November 28, 2017 at 6:09:17 AM UTC+8, Ned Batchelder wrote: > On 11/27/17 7:54 AM, Cai Gengyang wrote: > > Input : > > > > count = 0 > > > > if count < 5: > >print "Hello, I am an if statement and count is", count > > > > while count < 10: > >print "Hello, I am a while and count is", count > >count += 1 > > > > Output : > > > > Hello, I am an if statement and count is 0 > > Hello, I am a while and count is 0 > > Hello, I am a while and count is 1 > > Hello, I am a while and count is 2 > > Hello, I am a while and count is 3 > > Hello, I am a while and count is 4 > > Hello, I am a while and count is 5 > > Hello, I am a while and count is 6 > > Hello, I am a while and count is 7 > > Hello, I am a while and count is 8 > > Hello, I am a while and count is 9 > > > > The above input gives the output below. Why isn't the output instead : > > > > Hello, I am an if statement and count is 0 > > Hello, I am a while and count is 0 > > Hello, I am an if statement and count is 1 > > Hello, I am a while and count is 1 > > Hello, I am an if statement and count is 2 > > Hello, I am a while and count is 2 > > Hello, I am an if statement and count is 3 > > Hello, I am a while and count is 3 > > Hello, I am an if statement and count is 4 > > Hello, I am a while and count is 4 > > Hello, I am a while and count is 5 > > Hello, I am a while and count is 6 > > Hello, I am a while and count is 7 > > Hello, I am a while and count is 8 > > Hello, I am a while and count is 9 > > It's easy to imagine that this sets up a rule that remains in effect for the > rest of the program: > > â â â if count < 5: > â â â â â â â print "Hello, I am an if statement and count is", count > > But that's not how Python (and most other programming languages) works.â > Python > reads statements one after another, and executes them as it encounters > them.â > When it finds the if-statement, it evaluates the condition, and if it is true > *at that moment*, it executes the contained statements.â Then it forgets all > about that if-statement, and moves on to the next statement. > > --Ned. Sure, so how would the code look like if I want the "if" statement to be nested inside the "while" loop and give me the result : Hello, I am an if statement and count is 0 Hello, I am a while and count is 0 Hello, I am an if statement and count is 1 Hello, I am a while and count is 1 Hello, I am an if statement and count is 2 Hello, I am a while and count is 2 Hello, I am an if statement and count is 3 Hello, I am a while and count is 3 Hello, I am an if statement and count is 4 Hello, I am a while and count is 4 Hello, I am a while and count is 5 Hello, I am a while and count is 6 Hello, I am a while and count is 7 Hello, I am a while and count is 8 Hello, I am a while and count is 9 -- https://mail.python.org/mailman/listinfo/python-list
While, If, Count Statements
Input : count = 0 if count < 5: print "Hello, I am an if statement and count is", count while count < 10: print "Hello, I am a while and count is", count count += 1 Output : Hello, I am an if statement and count is 0 Hello, I am a while and count is 0 Hello, I am a while and count is 1 Hello, I am a while and count is 2 Hello, I am a while and count is 3 Hello, I am a while and count is 4 Hello, I am a while and count is 5 Hello, I am a while and count is 6 Hello, I am a while and count is 7 Hello, I am a while and count is 8 Hello, I am a while and count is 9 The above input gives the output below. Why isn't the output instead : Hello, I am an if statement and count is 0 Hello, I am a while and count is 0 Hello, I am an if statement and count is 1 Hello, I am a while and count is 1 Hello, I am an if statement and count is 2 Hello, I am a while and count is 2 Hello, I am an if statement and count is 3 Hello, I am a while and count is 3 Hello, I am an if statement and count is 4 Hello, I am a while and count is 4 Hello, I am a while and count is 5 Hello, I am a while and count is 6 Hello, I am a while and count is 7 Hello, I am a while and count is 8 Hello, I am a while and count is 9 -- https://mail.python.org/mailman/listinfo/python-list
Re: While, If, Count Statements
On 11/27/17 7:54 AM, Cai Gengyang wrote: > Input : > > count = 0 > > if count < 5: >print "Hello, I am an if statement and count is", count > > while count < 10: >print "Hello, I am a while and count is", count >count += 1 > > Output : > > Hello, I am an if statement and count is 0 > Hello, I am a while and count is 0 > Hello, I am a while and count is 1 > Hello, I am a while and count is 2 > Hello, I am a while and count is 3 > Hello, I am a while and count is 4 > Hello, I am a while and count is 5 > Hello, I am a while and count is 6 > Hello, I am a while and count is 7 > Hello, I am a while and count is 8 > Hello, I am a while and count is 9 > > The above input gives the output below. Why isn't the output instead : > > Hello, I am an if statement and count is 0 > Hello, I am a while and count is 0 > Hello, I am an if statement and count is 1 > Hello, I am a while and count is 1 > Hello, I am an if statement and count is 2 > Hello, I am a while and count is 2 > Hello, I am an if statement and count is 3 > Hello, I am a while and count is 3 > Hello, I am an if statement and count is 4 > Hello, I am a while and count is 4 > Hello, I am a while and count is 5 > Hello, I am a while and count is 6 > Hello, I am a while and count is 7 > Hello, I am a while and count is 8 > Hello, I am a while and count is 9 It's easy to imagine that this sets up a rule that remains in effect for the rest of the program: â â â if count < 5: â â â â â â â print "Hello, I am an if statement and count is", count But that's not how Python (and most other programming languages) works.â Python reads statements one after another, and executes them as it encounters them.â When it finds the if-statement, it evaluates the condition, and if it is true *at that moment*, it executes the contained statements.â Then it forgets all about that if-statement, and moves on to the next statement. --Ned. -- https://mail.python.org/mailman/listinfo/python-list
Re: While, If, Count Statements
On 27/11/2017 12:54, Cai Gengyang wrote: > > Input : > > count = 0 > > if count < 5: >print "Hello, I am an if statement and count is", count > > while count < 10: >print "Hello, I am a while and count is", count >count += 1 > > Output : > > Hello, I am an if statement and count is 0 > Hello, I am a while and count is 0 > Hello, I am a while and count is 1 > Hello, I am a while and count is 2 > Hello, I am a while and count is 3 > Hello, I am a while and count is 4 > Hello, I am a while and count is 5 > Hello, I am a while and count is 6 > Hello, I am a while and count is 7 > Hello, I am a while and count is 8 > Hello, I am a while and count is 9 > > The above input gives the output below. Why isn't the output instead : > > Hello, I am an if statement and count is 0 > Hello, I am a while and count is 0 > Hello, I am an if statement and count is 1 > Hello, I am a while and count is 1 > Hello, I am an if statement and count is 2 > Hello, I am a while and count is 2 > Hello, I am an if statement and count is 3 > Hello, I am a while and count is 3 > Hello, I am an if statement and count is 4 > Hello, I am a while and count is 4 > Hello, I am a while and count is 5 > Hello, I am a while and count is 6 > Hello, I am a while and count is 7 > Hello, I am a while and count is 8 > Hello, I am a while and count is 9 Because the if-statement is only executed once, then it does the loop. Try: count = 0 while count < 10: if count < 5: print "Hello, I am an if statement and count is", count print "Hello, I am a while and count is", count count += 1 -- https://mail.python.org/mailman/listinfo/python-list
Re: While, If, Count Statements
Cai Gengyangwrites: Statement 0: >count = 0 Statement 1: >if count < 5: > print "Hello, I am an if statement and count is", count Statement 2: >while count < 10: > print "Hello, I am a while and count is", count > count += 1 There are three statements here. They are executed in the given sequence. First, statement 0 binds the name â»countâ« to an object which has the int-value â»0â«. Next, statement 1 prints â»am an ifâ«. Now statement 1 was executed. It will never be revisited. It's history. Next, statement 2 will print "am a while" several times. It is separated from statement 2, like, totally. The statement 1 is in the remote past from where it never will return to be executed while the while loop is happily printing aways its "am a while" :-). Statement 1 is sad :-(, because it will never print no more. Never more. -- https://mail.python.org/mailman/listinfo/python-list
Re: While, If, Count Statements
On 11/27/17 7:54 AM, Cai Gengyang wrote: Input : count = 0 if count < 5: print "Hello, I am an if statement and count is", count while count < 10: print "Hello, I am a while and count is", count count += 1 Output : Hello, I am an if statement and count is 0 Hello, I am a while and count is 0 Hello, I am a while and count is 1 Hello, I am a while and count is 2 Hello, I am a while and count is 3 Hello, I am a while and count is 4 Hello, I am a while and count is 5 Hello, I am a while and count is 6 Hello, I am a while and count is 7 Hello, I am a while and count is 8 Hello, I am a while and count is 9 The above input gives the output below. Why isn't the output instead : Hello, I am an if statement and count is 0 Hello, I am a while and count is 0 Hello, I am an if statement and count is 1 Hello, I am a while and count is 1 Hello, I am an if statement and count is 2 Hello, I am a while and count is 2 Hello, I am an if statement and count is 3 Hello, I am a while and count is 3 Hello, I am an if statement and count is 4 Hello, I am a while and count is 4 Hello, I am a while and count is 5 Hello, I am a while and count is 6 Hello, I am a while and count is 7 Hello, I am a while and count is 8 Hello, I am a while and count is 9 It's easy to imagine that this sets up a rule that remains in effect for the rest of the program: if count < 5: print "Hello, I am an if statement and count is", count But that's not how Python (and most other programming languages) works. Python reads statements one after another, and executes them as it encounters them. When it finds the if-statement, it evaluates the condition, and if it is true *at that moment*, it executes the contained statements. Then it forgets all about that if-statement, and moves on to the next statement. --Ned. -- https://mail.python.org/mailman/listinfo/python-list
Re: While, If, Count Statements
On 27/11/2017 12:54, Cai Gengyang wrote: Input : count = 0 if count < 5: print "Hello, I am an if statement and count is", count while count < 10: print "Hello, I am a while and count is", count count += 1 Output : Hello, I am an if statement and count is 0 Hello, I am a while and count is 0 Hello, I am a while and count is 1 Hello, I am a while and count is 2 Hello, I am a while and count is 3 Hello, I am a while and count is 4 Hello, I am a while and count is 5 Hello, I am a while and count is 6 Hello, I am a while and count is 7 Hello, I am a while and count is 8 Hello, I am a while and count is 9 The above input gives the output below. Why isn't the output instead : Hello, I am an if statement and count is 0 Hello, I am a while and count is 0 Hello, I am an if statement and count is 1 Hello, I am a while and count is 1 Hello, I am an if statement and count is 2 Hello, I am a while and count is 2 Hello, I am an if statement and count is 3 Hello, I am a while and count is 3 Hello, I am an if statement and count is 4 Hello, I am a while and count is 4 Hello, I am a while and count is 5 Hello, I am a while and count is 6 Hello, I am a while and count is 7 Hello, I am a while and count is 8 Hello, I am a while and count is 9 Because the if-statement is only executed once, then it does the loop. Try: count = 0 while count < 10: if count < 5: print "Hello, I am an if statement and count is", count print "Hello, I am a while and count is", count count += 1 -- https://mail.python.org/mailman/listinfo/python-list
While, If, Count Statements
Input : count = 0 if count < 5: print "Hello, I am an if statement and count is", count while count < 10: print "Hello, I am a while and count is", count count += 1 Output : Hello, I am an if statement and count is 0 Hello, I am a while and count is 0 Hello, I am a while and count is 1 Hello, I am a while and count is 2 Hello, I am a while and count is 3 Hello, I am a while and count is 4 Hello, I am a while and count is 5 Hello, I am a while and count is 6 Hello, I am a while and count is 7 Hello, I am a while and count is 8 Hello, I am a while and count is 9 The above input gives the output below. Why isn't the output instead : Hello, I am an if statement and count is 0 Hello, I am a while and count is 0 Hello, I am an if statement and count is 1 Hello, I am a while and count is 1 Hello, I am an if statement and count is 2 Hello, I am a while and count is 2 Hello, I am an if statement and count is 3 Hello, I am a while and count is 3 Hello, I am an if statement and count is 4 Hello, I am a while and count is 4 Hello, I am a while and count is 5 Hello, I am a while and count is 6 Hello, I am a while and count is 7 Hello, I am a while and count is 8 Hello, I am a while and count is 9 -- https://mail.python.org/mailman/listinfo/python-list