python输出文本居中

编程入门 行业动态 更新时间:2024-10-05 03:25:27

python输出<a href=https://www.elefans.com/category/jswz/34/1771357.html style=文本居中"/>

python输出文本居中

问题是:For this program, the first line of input is an integer width. Then, there are some lines of text; the line "END" indicates the end of the text. For each line of text, you need to print out a centered version of it, by adding periods .. to the left and right, so that the total length of each line of text is width. (All input lines will have length at most width.) Centering means that the number of periods added to the left and added to the right should be equal if possible; if needed we allow one more period on the left than the right. For example, for input13

Text

in

the

middle!

END

正确的输出应该是

^{pr2}$

给出的提示是:For input line length of L, you should add (width-L)\\2 periods to the right side

以下是我目前为止的代码: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

我的第1行结果看起来像“……….Text..”而不是…..Text。。。。在

我的问题似乎是L。我不知道如何定义L。谢谢!在

更多推荐

python输出文本居中

本文发布于:2024-02-13 10:16:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1758390.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!