首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >居中相乘字符串,不打印空行Python 3

居中相乘字符串,不打印空行Python 3
EN

Stack Overflow用户
提问于 2017-02-18 10:48:27
回答 1查看 46关注 0票数 0

我需要在中间打印一些总宽度为60个字符的文本,而且当gpa低于3.25时,我不想打印空行。它工作正常,但当gpa低于3.25时,它会打印空行。

谁能帮帮我?

是否可以使用一个变量并将整个文本居中?

代码语言:javascript
复制
first_name, last_name = input("Please input your First Name and Last Name: ").split()
gpa = float(input("Please enter your GPA: "))


honor_message = ""

if gpa <=0:
    print("GPA can not be negative or 0")
else:         
    if 3.75 <= gpa <= 4.0:
        honor_message="\033[1;31;0m Excellent \x1b[0m"
    elif 3.5 <= gpa <= 3.74:
        honor_message = "\033[1;31;0m Good \x1b[0m"
    elif 3.25 <= gpa <= 3.49:
        honor_message = "\033[1;31;0m    Satisfactory \x1b[0m"
    else:
        honor_message

    print("Some text some text".center(60))
    print("Some text some text Some text some text".center(60))
    print("Some text some text".center(60))
    print(first_name.center(60).rstrip(), last_name)
    print("Some text some text".center(60))
    print("Some text some text".center(60))
    print(honor_message.center(60))
    print("Some text some text".center(60))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-18 11:42:19

测试消息是否有内容,如果有,则仅打印:

代码语言:javascript
复制
if honor_message:
    print(honor_message.center(60))

默认情况下,print()函数会添加一个新行。即使你的消息是空的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42310524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档