Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exercise': 0.04; 'elegant': 0.07; 'modified': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'random': 0.14; '"your': 0.16; '>>on': 0.16; 'balls': 0.16; 'btw': 0.16; 'incorrect': 0.16; 'range(0,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'reedy': 0.16; 'url:openbookproject': 0.16; 'width.': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'import': 0.22; 'aug': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'example.': 0.24; 'please?': 0.24; 'replace': 0.24; 'simpler': 0.24; 'mon,': 0.24; 'header:X -Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'sets': 0.30; 'code': 0.31; 'too.': 0.31; 'sets.': 0.31; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'up,': 0.60; 'numbers': 0.61; 'more': 0.64; 'different': 0.65; 'subject:This': 0.74; 'lotto': 0.84; 'repeat.': 0.84; 'url:php': 0.85; 'numbers:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: This formating is really tricky Date: Wed, 27 Aug 2014 09:16:43 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8be8.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409123816 news.xs4all.nl 2931 [2001:888:2000:d::a6]:60246 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77111 Seymore4Head wrote: > On Mon, 25 Aug 2014 18:22:35 -0400, Terry Reedy > wrote: > >>On 8/25/2014 4:14 PM, Seymore4Head wrote: >>> import random >>> sets=3 >>> for x in range(0, sets): >>> pb2=random.choice([1-53]) >> >>You want random.randint(1, 53) >>... >>> alist = sorted([pb1, pb2, pb3, pb4, pb5]) >>> print ("Your numbers: {} Powerball: {}".format(alist, pb6)) >>> >>> I am trying this example. The program works, but the numbers don't >>> line up if the number of digits are different sizes. >>> http://openbookproject.net/pybiblio/practice/wilson/powerball.php >> >>To get them to line up, you have to format each one to the same width. >> >>> Suggestion please? >>> BTW the exercise instructions say to use the choice function. >> >>import random >>sets=3 >> >>def ran53(): >> return random.randint(1, 53) >> >>f1 = '{:2d}' >>bform = "Your numbers: [{0}, {0}, {0}, {0}, {0}]".format(f1) >>pform = " Powerball: {0}".format(f1) >> >>for x in range(0, sets): >> balls = sorted(ran53() for i in range(5)) >> print(bform.format(*balls), pform.format(ran53())) >> > I modified your code to only use lotto numbers that don't repeat. I > am sure there is a more elegant way to this too. > > import random > sets=10 > print ("How many sets of numbers? ",sets) > > f1 = '{:2d}' > bform = "Your numbers: [{0}, {0}, {0}, {0}, {0}]".format(f1) > pform = " Powerball: {0}".format(f1) > > for x in range(0, sets): > balls = sorted(random.randint(1, 53) for i in range(5)) > if balls[0]!= balls[1] and balls[1]!= balls[2] and balls[2]!= > balls[3] and balls[3]!= balls[4]: A simpler test would be if len(set(balls)) == 5: > print(bform.format(*balls), pform.format(random.randint(1, > 42))) > sets=sets-1 It's not just elegance, the code is incorrect as it can print less than 10 sets. Replace the for loop with a while loop, and only decrease sets when you print a valid draw.