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


Groups > comp.lang.python > #54959

Re: Help me with Python please (picture)

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Help': 0.11; 'def': 0.12; 'wrote': 0.14; 'random': 0.14; '12:17,': 0.16; 'loop.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 50': 0.16; 'utc,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'import': 0.22; 'header:User- Agent:1': 0.23; 'number)': 0.24; 'replace': 0.24; 'first,': 0.26; 'skip:" 40': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'subject:please': 0.30; 'code': 0.31; 'getting': 0.31; 'probably': 0.32; "i'd": 0.34; 'subject: (': 0.35; 'subject:with': 0.35; "can't": 0.35; 'version': 0.36; 'charset:us-ascii': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'skip:* 10': 0.61; 'email addr:gmail.com': 0.63; 'overall': 0.69; 'subject:picture': 0.84; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re: Help me with Python please (picture)
Date Sat, 28 Sep 2013 17:16:57 +0000 (UTC)
References <7eb1cc5a-a25c-448e-9131-2f8b3ee60f96@googlegroups.com> <1f5d2dd8-877d-4d80-b3c3-d5a048556061@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 174.32.174.35
User-Agent XPN/1.2.6 (Street Spirit ; Linux)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.424.1380388639.18130.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380388639 news.xs4all.nl 15868 [2001:888:2000:d::a6]:44264
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54959

Show key headers only | 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