This formating is really tricky

Seymore4Head Seymore4Head at Hotmail.invalid
Mon Aug 25 21:16:27 EDT 2014


On Mon, 25 Aug 2014 20:51:36 -0400, Seymore4Head
<Seymore4Head at Hotmail.invalid> wrote:

>On Mon, 25 Aug 2014 18:22:35 -0400, Terry Reedy <tjreedy at udel.edu>
>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)
>>...
>I agree with you that random.randint would be what I would use too,
>but the instructions say to use choice.   The thing is, the next
>tutorial is rock paper scissors.
>
>Using "choice" for rock paper scissors does seem better as you can
>use:
>computer=random.choice(["Rock","Paper","Scissors"])
>
>BTW  In my trial and error endeavors I tried:
>random.choice([1,53]) for a shortcut.
>It seems to work.  That brings up another question.  What would you
>use if you only wanted those two choices?  1 and 53.
>I guess if you tried 53,1 it might work.
>
Actually, neither random.choice([1,53] or random.choice([53,1] work
for a shortcut.  Seeing the numbers sorted threw me off when I ran the
program.

>>>      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 ;=)
>
>I replied before I had a chance to try any of it.  Maybe the
>random.choice questions get answered after trying your suggestions.
>
>Thanks for you suggestions.  



More information about the Python-list mailing list