python输出文本居中

阅读: 评论:0

python输出文本居中

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。谢谢!在

本文发布于:2024-02-13 10:16:15,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/1715438217252293.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:文本   python
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23