This formating is really tricky

Peter Otten __peter__ at web.de
Mon Aug 25 18:48:52 EDT 2014


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))

Quoting the problem description: "The first five numbers are drawn from a 
drum containing 53 balls"

Thus no number should repeat in the first five. With your approach such 
repetitions can happen. The simplest solution is of course

random.sample(range(1, 54), 5)

but the OP will learn more when he tries to figure out how to get a correct 
solution with choice().





More information about the Python-list mailing list