[Tutor] while loop

Dave Angel davea at davea.name
Sat Mar 29 08:47:36 CET 2014


 Scott W Dunning <scott.w.d at cox.net> Wrote in message:
> 
> On Mar 28, 2014, at 9:54 PM, Scott W Dunning <scott.w.d at cox.net> wrote:
> 
>> Hello, I’m working on some practice exercises from my homework and I’m having some issues figuring out what is wanted.  
>> 
>> We’re working with the while loop and this is what the question states;
>> 
>> Write a function print_n that prints a string n times using iteration.
>> 
>> 	"""Print the string `s`, `n` times. 
>> 
>> 
>> This is also in the exercises and I’m not sure what it means and why it’s there.
>> 
>> assert isinstance(s, str)
>> assert isinstance(n, int)

What are you uncertain about,  assert or isinstance?  Such
 statements are frequently used to make sure the function
 arguments are of the right type. 

>
> 
> This is what I have so far but I’m not really sure it’s what the excersise is asking for?
> 
> n = 5
> def print_n(s, n):
>    while n > 0:
>        print s * n
> 
> print_n("hello", 10)
> 

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?



-- 
DaveA



More information about the Tutor mailing list