1
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

4

1 回答 1

2

我不知道您所说的“半”空格是什么意思,但我认为让您感到困惑的是您认为不应出现在数字之间的“额外”空格;我对吗?

如果是这种情况,那么这来自您print陈述中的最后一个逗号。您正在使用它来停止print在其输出后打印换行符——但您没有意识到的是,当它碰到那个逗号时它正在打印一个额外的空格。在 Python 命令提示符下试试这个:

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

我没有在“你好”后面加空格,那么它是从哪里来的呢?答:逗号。

您的困惑主要源于两件事:1) 使用错误的工具完成工作,以及 2) 对 Python 的输出方式缺乏深入了解(Python 初学者可以理解)。

问题 1 可以通过使用sys.stdout.write()代替来解决print。(请注意,import sys在使用它之前您需要这样做)。write()不添加任何空格或换行符;您需要自己指定空格和换行符。(换行符\n在 Python 字符串中,以防你忘记了)。因此,您的输出将更具可预测性。

问题 2 可以通过尽快通过http://docs.python.org/2/tutorial/inputoutput.html解决。请注意字符串方法的使用,例如.rjust():.format()习惯这些方法,你就会学会爱上它们。实际上,您很快就会想知道当有如此强大的功能可以为您完成时,您为什么还要手动进行这种字符串格式化。

希望这会有所帮助,并享受学习 Python 的乐趣!

于 2013-06-30T06:11:15.233 回答