Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77019

Re: This formating is really tricky

From Seymore4Head <Seymore4Head@Hotmail.invalid>
Newsgroups comp.lang.python
Subject Re: This formating is really tricky
Date 2014-08-25 23:10 -0400
Message-ID <13unv9psfs9nabv3llerpkm0grahkdn1ij@4ax.com> (permalink)
References <na5nv9l1slfkh4038ppc6779pbgajbbt19@4ax.com> <53fbf4da$0$29981$c3e8da3$5496439d@news.astraweb.com>
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


On Tue, 26 Aug 2014 12:45:45 +1000, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:

>Seymore4Head wrote:
>
>> import random
>> sets=3
>> for x in range(0, sets):
>>     pb1=random.choice([1,2,3,4,5,6,7,8,9,10 [...] 52,53])
>>     pb2=random.choice([1-53])
>
>You can avoid the annoyance of typing out long lists of sequential numbers
>by using the range() function. And you can avoid writing out the same value
>over and over again by giving it a name. So:
>
>numbers = list(range(1, 54))  # 1 is included, 54 is excluded.
>random.choice(numbers)
>
>will randomly select a number between 1 and 53 inclusive.
>
>random.choice([1-53]) doesn't do what you hope. It calculates 1-53 which
>gives -52, puts that inside a list [-52], then randomly chooses one of the
>items in the list -- the *only* item in the list, -52 every single time.
>
>Having said all that, *none* of the above code is relevant to your subject
>line. Yes, formatting is tricky, but nothing seen so far is about
>formatting.
>
>A well-designed program should be like a sandwich, not a stew. If I have a
>problem with a stew, there's no individual parts that I can easily point
>to. "The stew doesn't taste nice, please fix it." There's not a lot to go
>by. "Doesn't taste nice" could mean anything, and the only way to
>understand the stew is to consider *everything*, from the start to the
>finish, and that's hard work.
>
>But consider a sandwich: "When I put the bread on the tomato, it keeps
>falling off. Here is my tomato, why won't the bread stay on?" Answer: you
>forgot to slice the tomato. It's easy to see, because you can ignore the
>lettuce and the cheese and meat and the pickles and just look at the tomato
>in isolation of everything else, and it's obvious.
>
>In this case, the trick is to isolate the parts of your code that are to do
>with formatting, and ignore everything else. That might mean writing a new,
>smaller program. This will actually help you to understand what is going
>on, by digesting it in small chunks, rather than everything at once.
>
>So we come to the next part:
>
>>     alist = sorted([pb1, pb2, pb3, pb4, pb5])
>>     print ("Your numbers: {} Powerball: {}".format(alist, pb6))
>
>There is no need to show all the stuff about selecting random numbers, or
>that this is in a loop, or any of that. (Although, in this case you've
>hopefully inadvertently learned something by doing so, in another case you
>may just cause people reading to say "that's too hard, I'm too busy to read
>all that code" and your question goes unanswered.) But the *real* reason to
>learn to isolate the fault is that fault isolation is an essential skill
>that you, as a programmer, will need all through your career.
>
>Since the problem is with formatting, we can isolate the fault to just the
>formatting:
>
>print("Your numbers: {} Powerball: {}".format([1, 2, 3, 4, 5], 23))
>print("Your numbers: {} Powerball: {}".format([11, 21, 31, 41, 51], 7))
>
>And the results are:
>
>Your numbers: [1, 2, 3, 4, 5] Powerball: 23
>Your numbers: [11, 21, 31, 41, 51] Powerball: 7
>
>They certainly don't line up. Did you expect them to? How is the first print
>line supposed to know how far apart to space the numbers to match the
>second print line?
>
>Let's simplify even further:
>
>print("Powerball: {}".format(23))
>print("Powerball: {}".format(7))
>
>which gives:
>
>Powerball: 23
>Powerball: 7
>
>but we want the numbers to line up on the right, not the left. It's not
>obvious how to do that, the formatting mini-language is a bit obscure, but
>by reading the documentation, a bit of guesswork from half-remembered bits
>and pieces, trial and error, and/or asking someone else, I came up with:
>
>print("Powerball: {: >2}".format(23))
>print("Powerball: {: >2}".format(7))
>
>Powerball: 23
>Powerball:  7
>
>Success! The " >2" part of the format code inside the {} means to format the
>value using two columns, padding with spaces on the left if needed.
>
>So now we can format five columns for the five numbers, plus the powerball:
>
>template = "Numbers: {: >2} {: >2} {: >2} {: >2} {: >2}  Powerball: {: >2}"
>print(template.format(1, 2, 3, 4, 5, 23))
>print(template.format(11, 21, 31, 41, 51, 7))
>
>which gives:
>
>Numbers:  1  2  3  4  5  Powerball: 23
>Numbers: 11 21 31 41 51  Powerball:  7

Thank you  
I will give these a try.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

This formating is really tricky Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-25 16:14 -0400
  Re: This formating is really tricky Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-25 21:52 +0100
    Re: This formating is really tricky Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-25 23:12 -0400
  Re: This formating is really tricky Rustom Mody <rustompmody@gmail.com> - 2014-08-25 14:05 -0700
    Re: This formating is really tricky Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-25 23:14 -0400
      Re: This formating is really tricky Larry Hudson <orgnut@yahoo.com> - 2014-08-26 23:46 -0700
        Re: This formating is really tricky Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-27 09:36 -0400
  Re: This formating is really tricky Terry Reedy <tjreedy@udel.edu> - 2014-08-25 18:22 -0400
    Re: This formating is really tricky Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-25 20:51 -0400
      Re: This formating is really tricky Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-25 21:16 -0400
    Re: This formating is really tricky Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-26 15:31 -0400
      Re: This formating is really tricky Peter Otten <__peter__@web.de> - 2014-08-27 09:16 +0200
        Re: This formating is really tricky Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-27 09:35 -0400
  Re: This formating is really tricky Peter Otten <__peter__@web.de> - 2014-08-26 00:48 +0200
    Re: This formating is really tricky Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-25 20:52 -0400
      Re: This formating is really tricky Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-25 21:10 -0400
        Re: This formating is really tricky Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-08-26 18:17 +1200
          Re: This formating is really tricky Chris Angelico <rosuav@gmail.com> - 2014-08-26 16:22 +1000
        Re: This formating is really tricky alister <alister.nospam.ware@ntlworld.com> - 2014-08-26 09:27 +0000
          Re: This formating is really tricky Marko Rauhamaa <marko@pacujo.net> - 2014-08-26 12:32 +0300
            Re: This formating is really tricky alister <alister.nospam.ware@ntlworld.com> - 2014-08-26 10:32 +0000
      Re: This formating is really tricky Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-26 06:57 +0100
      Re: This formating is really tricky MRAB <python@mrabarnett.plus.com> - 2014-08-26 12:24 +0100
      Re: This formating is really tricky Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-26 09:28 -0400
      Re: This formating is really tricky Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-26 17:09 +0100
      Re: This formating is really tricky Chris Angelico <rosuav@gmail.com> - 2014-08-27 02:13 +1000
      Re: This formating is really tricky Peter Otten <__peter__@web.de> - 2014-08-26 18:28 +0200
      Re: This formating is really tricky Chris Angelico <rosuav@gmail.com> - 2014-08-27 02:33 +1000
      Re: This formating is really tricky Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-26 18:19 +0100
        Re: This formating is really tricky Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-08-27 09:39 +1200
  Re: This formating is really tricky Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-26 12:45 +1000
    Re: This formating is really tricky Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-25 23:10 -0400
  Re: This formating is really tricky Gene Heskett <gheskett@wdtv.com> - 2014-08-26 14:50 -0400

csiph-web