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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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. > 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