[Tutor] while loop
Dave Angel
davea at davea.name
Sun Mar 30 13:29:03 CEST 2014
Scott Dunning <swdunning at me.com> Wrote in message:
>
> On Mar 29, 2014, at 12:47 AM, Dave Angel <davea at davea.name> wrote:
>>
>> So did your code print the string 10 times? When asking for help,
>> it's useful to show what you tried, and what was expected, and
>> what actually resulted.
>>
>> You use * to replicate the string, but that wasn't what the
>> assignment asked for. So take out the *n part. You're supposed to
>> use iteration, specifically the while loop.
>>
>> Your while loop doesn't quit after 10 times, it keeps going. Can
>> you figure out why?
>
> This works without a break. Is this more a long the line of what the excercise was looking for you think?
>>
> def print_n(s, n):
> while n <= 10:
> print s
> n = n + 1
>
> print_n("hello\n", 0)
>
>
>
You're getting closer. Remember that the assignment shows your
function being called with 10, not zero. So you should have a
separate local variable, probably called I, which starts at
zero, and gets incremented each time.
The test in the while should be comparing them.
Note that the number of times is specified in top level code, and
implemented in the function. You should not have a literal 10 in
the function.
--
DaveA
More information about the Tutor
mailing list