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!