Re: python adds an extra half space when reading froma string or list -- back to the question
On 1 July 2013 20:32, Joel Goldstick joel.goldst...@gmail.com wrote: I copied the original question so that the rant on the other thread can continue. Let's keep this thread ontopic Thank you. I shall do the same below. Unfortunately I don't have high hopes that any progress will be made on this thread -- Charles Benoit hasn't made a single reply since his original post. On 29 June 2013 03:07, charles benoit feather.duster.kung...@gmail.com wrote: STUFF 1) You haven't asked a question. 2) You posted your code twice. That makes it look a lot harder and longer than it really is. 3) Give us a *minimal* reproducible test case. I currently just get: %~ python2 /tmp/nd.py Traceback (most recent call last): File /tmp/nd.py, line 12, in module finale_line2=finale_line NameError: name 'finale_line' is not defined This isn't the only problem. In other words, you've given us an unsolvable problem. Try again. -- http://mail.python.org/mailman/listinfo/python-list
Re: python adds an extra half space when reading froma string or list -- back to the question
On 07/01/2013 03:32 PM, Joel Goldstick wrote: 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[b])10: print( *(int(a[b])-space_count)),int(a[b]), space_count=int(a[b]) else: print( *(a[b]-space_count)),a[b], space_count=a[b]+1 Duplicate code removed 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 21 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.# But what was the expected output? And who cares? The code made no sense, was incomplete, and the posted question was nonsensical. If the OP has abandoned it, so should we. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: python adds an extra half space when reading froma string or list -- back to the question
On Tuesday, July 2, 2013 1:32:44 AM UTC+5:30, Dave Angel wrote: But what was the expected output? And who cares? The code made no sense, was incomplete, and the posted question was nonsensical. Yes in this specific instance all this is probably true. I believe however, that Joel's intent in reposting this is more global (and important) in its scope, viz: If this list persists in the current unhealthy state which it is experiencing, authentic noob questions will get buried in mountains of bullshit. Note: I find Joshua's answer fine given the question. If the OP has abandoned it, so should we. If you were a noob-OP who asked that question and the result unfolded as it has, what would you do? -- http://mail.python.org/mailman/listinfo/python-list
Re: python adds an extra half space when reading froma string or list -- back to the question
On 07/01/2013 05:16 PM, rusi wrote: On Tuesday, July 2, 2013 1:32:44 AM UTC+5:30, Dave Angel wrote: SNIP Yes in this specific instance all this is probably true. I believe however, that Joel's intent in reposting this is more global (and important) in its scope, viz: If this list persists in the current unhealthy state which it is experiencing, authentic noob questions will get buried in mountains of bullshit. Note: I find Joshua's answer fine given the question. If the OP has abandoned it, so should we. If you were a noob-OP who asked that question and the result unfolded as it has, what would you do? Point well-taken. So I'll see what I can do here. I'll put comments on lines I had to add or change. finale_line = []#missing initialization lot_number = 99 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 #not referenced 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[b])10: print( *(int(a[b])-space_count)),int(a[b]), space_count=int(a[b]) else: pass #print( *(a[b]-space_count)),a[b], #dead code #space_count=a[b]+1#dead code Since all the numbers are butted together in the string line_out, the later logic is iterating over digits, which cannot be bigger than 9. So the else clause is nonsensical. Even if they were to run, they'd give runtime errors. #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.# No clue what that means. What numbers are between what lines? And what is a normal print? Presumably the intent was to somehow do a variable spacing of those digits. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: python adds an extra half space when reading froma string or list -- 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[b])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/ -- http://mail.python.org/mailman/listinfo/python-list