This formating is really tricky
Terry Reedy
tjreedy at udel.edu
Mon Aug 25 18:22:35 EDT 2014
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()))
> BTW the exercise instructions say to use the choice function.
I am not a fan of exercises that say to do something the wrong way, but
if you really had to,
n54 = [i for i in range(1, 54)]
random.choice(n54)
An alternative to choosing numbers is to choose from 2-char number strings.
n53 = ['%2d' % i for i in range(1, 54)]
But then you have to figure out how to avoid having 6 pairs of quotes in
the output ;=)
--
Terry Jan Reedy
More information about the Python-list
mailing list