简体   繁体   中英

why is python generating a half space when printing from a string or list?

finale_line=[]
print type(finale_line)#just checking#
lot_number=()
number_drawn=()
def load():  
    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        #making sure it's a string at this point#
        finale_line.append(line_out)                
finale_line2=finale_line                        

load()


print finale_line  #again just checking#
print(" "*4),
for n in range(1,21):
    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

I apologize for not actually phrasing a "?" This would have cost me on Jeopardy. 2: I posted twice because I thought the first one was messed up.And 3: The code posted was an old defunct version but what worries me is you got a different error message than I did. I'm not sure this version will yield the same results on your machine. This has been an on-going problem with python.

>>>
<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 
1     2   3   4   5 
2    1   2   3   4 
3      3   4   5   6
>>> 

I realize there is problem with my logic in the spacing but I think this can be fixed.What I don't understand is where the half space is coming from. Or maybe a completely different approach is required. Thank you for responding and I don't wish to waste anyone's time but it greatly appreciated

I don't know what you mean by a "half" space, but I think what's confusing you is the "extra" space that you think shouldn't be showing up in between your numbers; am I right?

If that's the case, then that's coming from the final comma in your print statements. You're using it to stop print from printing a newline after its output -- but what you don't realize is that it's printing an extra space when it hits that comma. Try this at the Python command prompt:

>>> def hello():
...     print "Hello",
...     print "world"
... 
>>> hello()
Hello world

I didn't put a space after "Hello", so where did it come from? Answer: the comma.

Your confusion is mostly stemming from two things: 1) using the wrong tool for the job, and 2) a lack of in-depth understanding of how Python does output (understandable in a Python beginner).

Problem 1 can be solved by using sys.stdout.write() instead of print . (Do note that you'll need to import sys before you can use it). write() does not add any spaces or newlines; you'll need to specify the spaces and the newlines yourself. (Newlines are \\n in Python strings, in case you've forgotten). So your output will be more predictable.

Problem 2 can be solved by working your way through http://docs.python.org/2/tutorial/inputoutput.html as soon as possible. Notice the use of string methods like .rjust() and .format() : get used to those methods and you'll learn to love them. In fact, you'll soon wonder why in the world you would ever want to do this sort of string formatting by hand anymore, when such powerful functions are available to do it for you.

Hope this helps, and enjoy learning Python!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM