Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54959

Re: Help me with Python please (picture)

From Dave Angel <davea@davea.name>
Subject Re: Help me with Python please (picture)
Date 2013-09-28 17:16 +0000
References <7eb1cc5a-a25c-448e-9131-2f8b3ee60f96@googlegroups.com> <1f5d2dd8-877d-4d80-b3c3-d5a048556061@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.424.1380388639.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 28/9/2013 12:17, dvghana@gmail.com wrote:

> On Saturday, September 28, 2013 12:43:42 AM UTC, jae...@gmail.com wrote:
 
 
>> 
>> 
>> 
>> Can't seem to be getting an output.
  <snip>

> Overall I wrote my own version of the code and this is what I got:
>
>
> ******************
> import string
> import random
>
> def random_characters(number):
>     i = 0
>     new_string = []
>
>     while (i < number) :
>         new_string.append(random.choice(string.ascii_lowercase))
>         i = i + 1
>     return "".join(new_string)
>
>
> print(random_characters(3))
> *******

First, I'd clean up the variable name, and use a for loop instead of a
while loop.

import string
import random

def random_characters(number):
    new_list = []
    for i in range(number):
        new_list.append(random.choice(string.ascii_lowercase))
    return "".join(new_list)


print(random_characters(8))

Then I'd probably replace the function body with:

def random_characters(number):
    return "".join([random.choice(string.ascii_lowercase) for i in
range(number)])

-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Help me with Python please (picture) jae655@gmail.com - 2013-09-27 17:43 -0700
  Re: Help me with Python please (picture) Gary Herron <gary.herron@islandtraining.com> - 2013-09-27 18:11 -0700
  Re: Help me with Python please (picture) Dave Angel <davea@davea.name> - 2013-09-28 01:18 +0000
  Re: Help me with Python please (picture) Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-28 02:18 +0000
  Re: Help me with Python please (picture) John Ladasky <john_ladasky@sbcglobal.net> - 2013-09-27 21:03 -0700
  Re: Help me with Python please (picture) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-28 05:18 +0000
    Re: Help me with Python please (picture) Chris Angelico <rosuav@gmail.com> - 2013-09-28 15:46 +1000
  Re: Help me with Python please (picture) dvghana@gmail.com - 2013-09-28 09:17 -0700
    Re: Help me with Python please (picture) Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-28 12:50 -0400
      Re: Help me with Python please (picture) Bob Martin <bob.martin@excite.com> - 2013-09-29 08:07 +0100
    Re: Help me with Python please (picture) Dave Angel <davea@davea.name> - 2013-09-28 17:16 +0000

csiph-web