python adds an extra half space when reading froma string or list --back to the question

Discussion in 'Python' started by Joel Goldstick, Jul 1, 2013.

  1. I copied the original question so that the rant on the other thread
    can continue. Let's keep this thread ontopic


    number_drawn=()
    def load(lot_number,number_drawn):
    first=input("enter first lot: ")
    last=input("enter last lot: ")
    for lot_number in range(first,last):
    line_out=str(lot_number)
    for count in range(1,5):
    number_drawn=raw_input("number: ")
    line_out=line_out+(number_drawn)
    print line_out
    finale_line.append(line_out)
    finale_line2=finale_line

    load(lot_number,number_drawn)


    print finale_line
    print(" "*4),
    for n in range(1,41):
    print n, #this is to produce a line of numbers to compare to
    output#
    for a in finale_line:
    print"\n",
    print a[0]," ",
    space_count=1
    for b in range(1,5):
    if int(a)<10:
    print(" "*(int(a)-space_count)),int(a),
    space_count=int(a)
    else:
    print(" "*(a-space_count)),a,
    space_count=a+1







    number_drawn=()
    def load(lot_number,number_drawn):
    first=input("enter first lot: ")
    last=input("enter last lot: ")
    for lot_number in range(first,last):
    line_out=str(lot_number)
    for count in range(1,5):
    number_drawn=raw_input("number: ")
    line_out=line_out+(number_drawn)
    print line_out
    finale_line.append(line_out)
    finale_line2=finale_line

    load(lot_number,number_drawn)


    print finale_line
    print(" "*4),
    for n in range(1,41):
    print n, #this is to produce a line of numbers to compare to
    output#
    for a in finale_line:
    print"\n",
    print a[0]," ",
    space_count=1
    for b in range(1,5):
    if int(a)<10:
    print(" "*(int(a)-space_count)),int(a),
    space_count=int(a)
    else:
    print(" "*(a-space_count)),a,
    space_count=a+1








    this generates
    <type 'list'>
    enter first lot: 1
    enter last lot: 4
    number: 2
    number: 3
    number: 4
    number: 5
    12345
    number: 1
    number: 2
    number: 3
    number: 4
    21234
    number: 3
    number: 4
    number: 5
    number: 6
    33456
    ['12345', '21234', '33456']
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    27 28 29 30 31 32 33 34 35 36 37 38 39 40
    1 2 3 4 5
    2 1 2 3 4
    3 3 4 5 6
    >#as you can see many numbers are between the lines of a normal print#

    #I thought this was due to "white space" int he format .So I tried a list
    of strings and got the same results.#



    --
    Joel Goldstick
    http://joelgoldstick.com
    Joel Goldstick, Jul 1, 2013
    #1
    1. Advertising

  2. Joel Goldstick

    alex23 Guest

    Re: python adds an extra half space when reading froma string orlist -- back to the question

    On 2/07/2013 5:32 AM, Joel Goldstick wrote:
    > I copied the original question so that the rant on the other
    > thread can continue. Let's keep this thread ontopic


    You've included the same set of code twice. Also, it doesn't run as is,
    so you haven't reduced it to a minimal working example for us to test.

    Python's print adds a space where there's a comma, try this at the
    interactive prompt:

    >>> print 'a','b'

    a b

    I don't think you're taking this into account. There are quite a few
    other problems with your code, though:

    > number_drawn=()
    > def load(lot_number,number_drawn):


    You've assigned `number_drawn` to a tuple, which is an immutable type.
    Within the `load` function you then do:

    > number_drawn=raw_input("number: ")


    Which just re-assigns `number_drawn` to the output of the `raw_input`
    function _within the function itself_. The value isn't available
    outside of the function, if that is what you're intending. You also
    haven't defined `lot_number` in your code, and again you re-assign it
    within the body of the `load` function:

    > for lot_number in range(first,last):


    Which has no impact on any global definition. Since you're not actually
    passing values into the function you can probably do without both
    arguments and just go with:

    def load():

    > first=input("enter first lot: ")
    > last=input("enter last lot: ")


    You should never use `input`, it evaluates the expression entered.
    Always use `raw_input` unless you absolutely know what you're doing.

    > for lot_number in range(first,last):


    `range` will start at `first` and finish _at but not including `last`.
    If you want 4 lines when you enter first=1 and last=4, then you need to
    increment `last` by 1:

    for lot_number in range(first,last+1):

    > finale_line.append(line_out)


    Where is `finale_line` defined? You haven't shown it but my guess is
    you've made it an empty list. You're also not returning anything from
    the function, which implies you're relying on global scope to hold the
    result. This is bad practice. You should start your function with:

    finale_line = []

    And then end it with:

    return finale_line

    Which would allow you to replace:

    > finale_line2=finale_line
    > load(lot_number,number_drawn)


    With:

    finale_line = load()

    > for a in finale_line:
    > print"\n",
    > print a[0]," ",
    > space_count=1
    > for b in range(1,5):


    This condition will _always_ be true:

    > if int(a)<10:


    Because of the way you're stepping through `finale_line`, you're only
    ever looking at a single character, so that value will always be from 0
    to 9. You clearly want to allow for double digit numbers, so not storing
    them all as a big string would be a good start. You're dealing
    with numbers, so hold them as numbers in a list.

    Here's a more flexible approach that I think does what you want:

    import os

    def load_numbers():
    first=int(raw_input("enter first lot: "))
    last=int(raw_input("enter last lot: "))
    finale_line = []
    for lot_number in range(first,last+1):
    line_out = []
    for count in range(1,5):
    number_drawn=raw_input("lot %d, number %d: " %
    (lot_number, count))
    line_out.append(number_drawn)
    finale_line.append((lot_number, line_out))
    return finale_line

    finale_line = load_numbers()

    # get space needed from the maximum number entered
    largest_number = max(
    number for row,numbers in finale_line for number in numbers)
    space_count = len(str(largest_number))

    # get no. of columns from the first set of numbers
    columns = len(finale_line[0][1])
    # space to cover the lot numbers
    print ' ',
    # print the columns
    for column in xrange(1, columns+1):
    spacing = space_count - len(str(column))
    print '%s%d' % (' '*spacing, column),

    for lot, numbers in finale_line:
    print os.linesep, # use the EOL used by the current
    # operating system
    print lot,
    for number in numbers:
    spacing = space_count - len(str(number))
    print '%s%d' % (' '*spacing, number),

    However, if you want to make your life a _lot_ easier when printing
    tabular data, I highly recommend using a library dedicated to just
    that, something like:

    https://code.google.com/p/prettytable/
    alex23, Jul 2, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christoph Haas
    Replies:
    4
    Views:
    312
    Christoph Haas
    Oct 2, 2005
  2. Joshua Landau
    Replies:
    0
    Views:
    102
    Joshua Landau
    Jun 29, 2013
  3. Mark Lawrence
    Replies:
    106
    Views:
    696
    Antoon Pardon
    Jul 8, 2013
  4. Joshua Landau
    Replies:
    0
    Views:
    106
    Joshua Landau
    Jun 29, 2013
  5. Dave Angel
    Replies:
    2
    Views:
    98
    Dave Angel
    Jul 2, 2013
Loading...

Share This Page