On 03/29/2014 01:18 AM, Scott W Dunning wrote:
On Mar 28, 2014, at 9:54 PM, Scott W Dunning <scott....@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)


Any help is greatly appreciated!

Scott
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)

Scott,

Are required to use a while loop?

Two ways to solve this, while loop and for in range loop

while loop
n = 5
s = 'some string'

def while_print(s, n):
    index = o
    while index < n:
        print(index, s)
        index += 1

def alternative_print(s, n):
    for index in range(0, n):
        print(index, s)

The while loop requires that a counter variable or some loop exit condition so you do not end up in an endless loop. This is a common problem with while loops in programming, not just Python. So, if there is alternative method of looping available, good programming practice is to avoid a while loop in any language. The for loop version automatically iterates over the range from 0 to n-1 (5 times). I included the index variable in the print to show the increase of the index.

The results for both:
0 some string
1 some string
2 some string
3 some string
4 some string

--
Jay Lozier
jsloz...@gmail.com

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

Reply via email to