Re: Nested Loop Code Help
On 27/01/20 1:53 PM, Richard Damon wrote: On 1/26/20 6:52 PM, DL Neil via Python-list wrote: On 27/01/20 4:15 AM, ferzan saglam wrote: Hello people, I have written the code below which works fine, but it has one small problem. Instead of printing one (x) on the first line, it prints two. I have tried everything in my knowledge, but cannot fix the problem. Thanks for any help in advance. for x in range ( 0, 10): stars = 'x' count = 0 while count < x: stars = stars + 'x' count = count + 1 print (stars) These loops are serial, ie one after the other, and not "nested" (one 'inside' the other) - or the email messed-up the indentation. However, why "nest" or have more than one loop, in any case? >>> for i in range( 0, 10 ): ... print( "*"*i ) ... * ** *** * ** *** * First, your answer doesn't solve his problem, as his expected was lines of 1 to 10 stars, not 0 to 9. Second, this smells a bit like homework, and if they haven't learned the results of string times integer, then using that operation wouldn't be in their tool kit, so having a loop to build that operator makes sense. The reply to the first criticism is answered in the second - it only looks like the answer. Notice also, that the character used, whilst a "star", is not what the OP wanted either. Also, please recall that the OP has already accepted one answer (whether the 'right' one, or not, is another matter). If it was 'homework', what are the chances that (s)he will go back and 'change everything' after saying "done"? Would you? Would I? A while back we had a rash of folk asking beginner questions. Not one to whom I posed the 'is this homework?' question, responded. Also, there is the Python Tutor list... That said, there is no telling in which order people learn particular aspects of a topic [Python in this case]. A quick check of the reference I use, shows a listing of string functions (and other objects) before reaching looping constructs. Learning that there are different ways of approaching the same problem/solution is valuable learning. How about taking a string of ten 'stars' and printing progressively longer slices thereof, on successive lines? (that book has slices (of strings and lists) before loops too!) Did anyone suggest that it seemed a little odd to use both a for-loop and a while-loop? Why not both for-loops (there are finite starting and finishing points (and increments) after-all)? BTW your earlier response extremely constructive. Have you seen Philip Guo's http://pythontutor.com/ ? It illustrates what we learned as 'paper computers', with all the advantages of dynamism! The site now makes it possible for learners to 'go' as far as they are able, and then to call for help; and for someone to peer-review, comment, and/or pair-program(me) right there with them! -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested Loop Code Help
On 1/26/20 6:52 PM, DL Neil via Python-list wrote: On 27/01/20 4:15 AM, ferzan saglam wrote: Hello people, I have written the code below which works fine, but it has one small problem. Instead of printing one (x) on the first line, it prints two. I have tried everything in my knowledge, but cannot fix the problem. Thanks for any help in advance. for x in range ( 0, 10): stars = 'x' count = 0 while count < x: stars = stars + 'x' count = count + 1 print (stars) These loops are serial, ie one after the other, and not "nested" (one 'inside' the other) - or the email messed-up the indentation. However, why "nest" or have more than one loop, in any case? >>> for i in range( 0, 10 ): ... print( "*"*i ) ... * ** *** * ** *** * First, your answer doesn't solve his problem, as his expected was lines of 1 to 10 stars, not 0 to 9. Second, this smells a bit like homework, and if they haven't learned the results of string times integer, then using that operation wouldn't be in their tool kit, so having a loop to build that operator makes sense. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested Loop Code Help
On 27/01/20 4:15 AM, ferzan saglam wrote: Hello people, I have written the code below which works fine, but it has one small problem. Instead of printing one (x) on the first line, it prints two. I have tried everything in my knowledge, but cannot fix the problem. Thanks for any help in advance. for x in range ( 0, 10): stars = 'x' count = 0 while count < x: stars = stars + 'x' count = count + 1 print (stars) These loops are serial, ie one after the other, and not "nested" (one 'inside' the other) - or the email messed-up the indentation. However, why "nest" or have more than one loop, in any case? >>> for i in range( 0, 10 ): ... print( "*"*i ) ... * ** *** * ** *** * -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested Loop Code Help
On 1/26/20 6:11 PM, Greg Ewing wrote: On 27/01/20 4:15 am, ferzan saglam wrote: for x in range ( 0, 10): stars = 'x' count = 0 By the way, this 'for' loop is unnecessary. The end result is just to give initial values to three names. You don't need a loop at all for that, just three assignment statements. His thinking MIGHT have been that this for loop be the main loop that prints the 10 lines (i.e the following loop be within this loop). In that case, the assignment statements in the loop make sense. In that case, the print call wouldn't be in the while loop, which would add x 'x's to the string. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested Loop Code Help
On 27/01/20 4:15 am, ferzan saglam wrote: for x in range ( 0, 10): stars = 'x' count = 0 By the way, this 'for' loop is unnecessary. The end result is just to give initial values to three names. You don't need a loop at all for that, just three assignment statements. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested Loop Code Help
On 1/26/20 10:15 AM, ferzan saglam wrote: Hello people, I have written the code below which works fine, but it has one small problem. Instead of printing one (x) on the first line, it prints two. I have tried everything in my knowledge, but cannot fix the problem. Thanks for any help in advance. for x in range ( 0, 10): stars = 'x' count = 0 while count < x: stars = stars + 'x' count = count + 1 print (stars) Output I am trying to get Output I am getting x xx xxxxx xxx x x xx xxxxx xxx x x xx xx First skill is to learn to act like an interpreter, and execute the instructions one by one. First we execute the for x in range (0, 10): statement which will set x to 0 Then we set stars to 'x' Then we set count to 0, since the while outdents, that ends the loop and we repeat it with x having values 1, 2, 3, and up to 9 THEN we go to the bottom loop. Count is 0, x is 9 so the while is satisfied, so we do a loop stars = stars + 'x' which is 'xx' count = count + 1 which is 1 print(stars) prints 'xx' that ends one pass thru the while loop, so we do the test again Count is 1, x is 9, so the while is satisfied, so we do a loop, stars = stars + 'x', which is 'xxx' count = count + 1, which is 2 print(stars) prints 'xxx' this continues until count reaches 9 (the value left in x) at which it stops. Thus the loop ran 9 times (for starting values of count = 0, 1, 2, 3, 4, 5, 6, 7, 8 Think about what you wanted to do and what the code actually did. The first for x in range (0, 10) doesn't really do what I think you wanted, did you mean for the second loop to be nested in it? If you do nest the second loop, you probably don't want the print at the end part of the second loop. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested Loop Code Help
On Sunday, January 26, 2020 at 3:26:40 PM UTC, Dan Purgert wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA256 > > ferzan saglam wrote: > > Hello people, I have written the code below which works fine, but it > > has one small problem. Instead of printing one (x) on the first line, > > it prints two. > > I have tried everything in my knowledge, but cannot fix the problem. > > Thanks for any help in advance. > > either take the initial "stars = x" out, or increment stars after you > print it. Either approach should give you the result you want. > > -BEGIN PGP SIGNATURE- > > iQEzBAEBCAAdFiEEBcqaUD8uEzVNxUrujhHd8xJ5ooEFAl4tr5sACgkQjhHd8xJ5 > ooGt+AgArjEk3CIPLvX36SBhZjnjOCdpTXpvzjZDePgMcAJT3WZTweIVmF+JyV6w > xS7kSI/pjmReP/BTH6XGdcJiqsyKeOh5C1D5HgCYFNsuXcegCd1j7PvKlQ/3tVFz > MZUhT46PtWK04Er/Hj4IHUdvP+IRu5HNeUozcWHPlSV9VJmfOd5e94s/cV7Mx5HA > d1p+GHrhS3VDIqY0MBxm3r9A6ciKjys328YpiUpIRfTIX8uUEYWPhNRr3UcIHLBC > ilgZUV5evyr5CXFIET5UKEw4QD0eMLy3T6o2VqR5HsrgAgFW95RTWXNA+qWw+spV > t/grPnMpZ8Cjw5TTbmFYYIrvEyJ+WQ== > =ern6 > -END PGP SIGNATURE- > > -- > |_|O|_| > |_|_|O| Github: https://github.com/dpurgert > |O|O|O| PGP: 05CA 9A50 3F2E 1335 4DC5 4AEE 8E11 DDF3 1279 A281 Thanks for the help Dan, your suggestion worked. -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested Loop Code Help
-BEGIN PGP SIGNED MESSAGE- Hash: SHA256 ferzan saglam wrote: > Hello people, I have written the code below which works fine, but it > has one small problem. Instead of printing one (x) on the first line, > it prints two. > I have tried everything in my knowledge, but cannot fix the problem. > Thanks for any help in advance. either take the initial "stars = x" out, or increment stars after you print it. Either approach should give you the result you want. -BEGIN PGP SIGNATURE- iQEzBAEBCAAdFiEEBcqaUD8uEzVNxUrujhHd8xJ5ooEFAl4tr5sACgkQjhHd8xJ5 ooGt+AgArjEk3CIPLvX36SBhZjnjOCdpTXpvzjZDePgMcAJT3WZTweIVmF+JyV6w xS7kSI/pjmReP/BTH6XGdcJiqsyKeOh5C1D5HgCYFNsuXcegCd1j7PvKlQ/3tVFz MZUhT46PtWK04Er/Hj4IHUdvP+IRu5HNeUozcWHPlSV9VJmfOd5e94s/cV7Mx5HA d1p+GHrhS3VDIqY0MBxm3r9A6ciKjys328YpiUpIRfTIX8uUEYWPhNRr3UcIHLBC ilgZUV5evyr5CXFIET5UKEw4QD0eMLy3T6o2VqR5HsrgAgFW95RTWXNA+qWw+spV t/grPnMpZ8Cjw5TTbmFYYIrvEyJ+WQ== =ern6 -END PGP SIGNATURE- -- |_|O|_| |_|_|O| Github: https://github.com/dpurgert |O|O|O| PGP: 05CA 9A50 3F2E 1335 4DC5 4AEE 8E11 DDF3 1279 A281 -- https://mail.python.org/mailman/listinfo/python-list
Nested Loop Code Help
Hello people, I have written the code below which works fine, but it has one small problem. Instead of printing one (x) on the first line, it prints two. I have tried everything in my knowledge, but cannot fix the problem. Thanks for any help in advance. for x in range ( 0, 10): stars = 'x' count = 0 while count < x: stars = stars + 'x' count = count + 1 print (stars) Output I am trying to get Output I am getting x xx xxxxx xxx x x xx xxxxx xxx x x xx xx -- https://mail.python.org/mailman/listinfo/python-list