Re: [Tutor] while loop

2014-03-30 Thread Scott Dunning

On Mar 28, 2014, at 10:36 PM, Ben Finney  wrote:
> 
> A good programming exercise will show an example input and the expected
> output, to give an unambiguous test case. Does the homework have that?
This is what the exercise has as examples…


"""Print the string `s`, `n` times.

Parameters
--
s -- A string
n -- an integer, the number of times to
 print `s'

Examples


>>> print_n("hello", 3)
hello
hello
hello

>>> print_n("bye", 0)

>>> print_n("a", 6)
a
a
a
a
a
a

"""
assert isinstance(s, str)
assert isinstance(n, int)

#TODO: Implement the function

> 
> If not, you're unfortunately left to your own interpretation of what the
> requirements mean.
> 
I’m not sure what assert isinstance means?  

This is what I have, it works but I’m not sure it’s what the exercise is asking 
for.

n = 5
def print_n(s, n):
while n > 0:
print s * n
break

print_n("hello\n", 10)



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] while loop

2014-03-30 Thread Scott Dunning

On Mar 29, 2014, at 12:47 AM, Dave Angel  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?  


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] while loop

2014-03-30 Thread Scott Dunning

On Mar 29, 2014, at 12:47 AM, Dave Angel  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)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] while loop

2014-03-30 Thread Alan Gauld

On 30/03/14 02:36, Scott Dunning wrote:


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?

Yes.


 while n <= 10:
 print s
 n = n + 1


Python while loops effectively come in two patterns:

1) while some condition
  do stuff
  modify the test condition

2) while True:
  if some condition:
 break
  else
 do stuff


The first version is actually the intended use of while from a computing 
science point of view.


The second one, which creates an infinite loop and then breaks
out of it is a bit of a kluge which pure structured programming
theory says is bad practice. It has become idiomatic in Python
however, because it often avoids another bad practice, namely
repeating yourself.

For example if we only used the first pattern we often
need to do this:

display_menu()
choice = input('pick a choice: ')
while choice != 'quit':
if choice == 'save':
   do_save()
elif choice == ...
display_menu()
choice = input('pick a choice: ')

Notice how we have to have the menu/input pair
both before and inside the loop.

We can avoid that using the infinite loop version:

while True:
display_menu()
choice = input('pick a choice: ')
if choice == 'quit'
   break
elif choice == 'save':
   do_save()
elif choice == ...

So we have effectively chosen the lesser of two evils.
Compromising on computer science purity is not unusual
in the real world of programming.

In your example there was no need to repeat code so
you could use the uncompromised, pure while loop with
an effective test condition. In that case you can and
should modify the test condition variables inside
the loop, which is what you did with the n = n+1 line.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] while loop

2014-03-30 Thread Dave Angel
 Scott Dunning  Wrote in message:
> 
> On Mar 29, 2014, at 12:47 AM, Dave Angel  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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor