Python逻辑 - 使用句点对齐文本

时间:2013-09-04 17:54:07

标签: python string logic

我正在考虑this intro to python course online

问题是:

  

对于此程序,第一行输入是整数宽度。然后,有一些文字; “END”行表示文本的结尾。对于每行文本,您需要通过向左和向右添加句点来打印出它的居中版本,以便每行文本的总长度为宽度。 (所有输入行的长度最多为宽度。)居中意味着如果可能,添加到左侧并添加到右侧的句点数应相等;如果需要,我们允许左侧比右侧多一个时段。例如,输入

13
Text
in
the
middle!
END

正确的输出是

.....Text....
......in.....
.....the.....
...middle!...

给出的提示是:

  

对于L的输入行长度,您应该向右侧添加(width-L)\\ 2个句点

到目前为止,这是我的代码:

width = int(input())
s1 = input()

periods_remain = width - len(s1)
L = periods_remain
periods_rtside = (width-L)//2
periods_leftside = width - periods_rtside
periods_rt_str = '.' * periods_rtside
periods_left_str = '.' * periods_leftside
line1 = periods_left_str + s1 + periods_rt_str

我的line1结果看起来像“........... Text ..”而不是..... Text ....

It can be run here

我的问题似乎是L.我不确定如何定义L.谢谢!

3 个答案:

答案 0 :(得分:4)

您可以使用str.center

>>> lis = ['Text', 'in', 'the', 'middle!', 'END']
>>> for item in lis:
...     print item.center(13, '.')
...     
.....Text....
......in.....
.....the.....
...middle!...
.....END.....

format

for item in lis:
    print format(item,'.^13')
...     
....Text.....
.....in......
.....the.....
...middle!...
.....END.....

代码的工作版本:

lis = ['Text', 'in', 'the', 'middle!', 'END']
width = 13
for s1 in lis:
    L = len(s1)                                    #length of line
    periods_rtside = (width - L)//2                #periods on the RHS
    periods_leftside = width - periods_rtside - L  #peroids on the LHS
    periods_rt_str = '.' * periods_rtside
    periods_left_str = '.' * periods_leftside
    line1 = periods_left_str + s1 + periods_rt_str
    print line1

<强>输出:

.....Text....
......in.....
.....the.....
...middle!...
.....END.....

答案 1 :(得分:0)

对于那些仍在努力解决这个棘手问题的人来说,这是我的代码在Python 3 shell中运行,即使它仍然在http://cscircles.cemc.uwaterloo.ca/8-remix/中失败

First_line = input("First input: ")
width = int(First_line)

while True:
    s1 = input("Second input: ")
    if s1 != 'END':
        L = len(s1)                                                             #length of line
        periods_rtside = (width - L)//2                                     #periods on the RHS
        periods_leftside = width - periods_rtside - L                   #periods on the LHS
        periods_rt_str = '.' * periods_rtside
        periods_left_str = '.' * periods_leftside
        line1 = periods_left_str + s1 + periods_rt_str
        print(line1)
    else:
        break

要使其在http://cscircles.cemc.uwaterloo.ca/8-remix/控制台中运行,您需要将前2行更改为

width = int(input())

和 s1到s1 = input()

并通过单击&#34;输入测试输入&#34;来提供您自己的测试输入。按钮

答案 2 :(得分:0)

对上述代码进行了一些小改动,并且它在平地机中起作用。

     width = int(input())
     s1 = input()
     while s1 != "END":
        L = len(s1)                                                                  
        periods_rtside = (width - L)//2                                     
        periods_leftside = width - periods_rtside - L                   
        periods_rt_str = '.' * periods_rtside
        periods_left_str = '.' * periods_leftside
        line1 = periods_left_str + s1 + periods_rt_str
        print(line1)
        s1 = input()