Saturday, September 28, 2013

Generate random characters against specific loop condition

Generate random characters against specific loop condition

I want to continue generating a single character at random as long as the
loop condition is not met. I was hoping my code below will work but it
doesn't:
import string
import random
def random_characters(number):
i = 0
new_string = ''
while (i < number) :
new_string = random.choice(string.ascii_lowercase)
i = i + 1
return new_string
random_characters(3)
When i run this code nothing happens and I don't get any feedback about
any error. I may be using return new_string in a wrong way but I am unable
to figure it out even if that is what I'm missing.

No comments:

Post a Comment