[Tutor] while loop

Scott Dunning swdunning at me.com
Sun Mar 30 01:51:17 CET 2014


On Mar 29, 2014, at 12:47 AM, Dave Angel <davea at davea.name> wrote:
> 
> What are you uncertain about,  assert or isinstance?  Such
> statements are frequently used to make sure the function
> arguments are of the right type. 
I’m not sure exactly what it’s doing.  I guess I need to read up on it again.
> 
>> 
>> 
>> 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. 
Yes it repeated forever, I put a break after the print statement.  
> 
> 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. 
That’s where I was confused, I wasn’t sure how to get the while loop to work just the n times and then stop.
> 
> Your while loop doesn't quit after 10 times, it keeps going.  Can
> you figure out why?
> 
I understand why it didn’t stop after 10 times, because I said while n is greater than 0 print s, and I have the value of n as 5 so it will never stop, that’s why I added a break after the print statement.  I’m sure there is a better way , I’m just not seeing it.  Any hints?  




More information about the Tutor mailing list