首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >有没有别的方法可以写这篇文章?我只知道打印函数,但我似乎不能像示例那样理解它

有没有别的方法可以写这篇文章?我只知道打印函数,但我似乎不能像示例那样理解它
EN

Stack Overflow用户
提问于 2019-06-20 02:40:10
回答 2查看 41关注 0票数 0

我似乎不能得到与示例相同的打印功能我已经使用了基本的打印,但它不会给我我想要的,而且逗号似乎没有分割它

python 2.7

代码语言:javascript
复制
print "NUCLEAR CORE UNSTABLE!!!, Quarantine is in effect. , Surrounding hamlets will be evacuated. , Anti-radiationsuits and iodine pills are mandatory."

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-20 03:22:17

您使用的是print语句,而不是函数,有几种方法可以做到这一点:

它使用三重引号字符串来保留换行符:

代码语言:javascript
复制
def printit():
    print """NUCLEAR CORE UNSTABLE!!!
Quarantine is in effect.
Surrounding hamlets will be evacuated.
Anti-radiationsuits and iodine pills are mandatory.
    """

要运行3次,只需:

代码语言:javascript
复制
for i in range(3):
    printit()

它使用了多条print语句:

代码语言:javascript
复制
def printit():
    print "NUCLEAR CORE UNSTABLE!!!"
    print "Quarantine is in effect."
    print "Surrounding hamlets will be evacuated."
    print "Anti-radiationsuits and iodine pills are mandatory.\n"

这只使用了一行,并嵌入了换行符:

代码语言:javascript
复制
def printit():
    print "NUCLEAR CORE UNSTABLE!!!\nQuarantine is in effect.\nSurrounding hamlets will be evacuated.\nAnti-radiationsuits and iodine pills are mandatory.\n"

但是,您提到了print函数,并抱怨逗号分隔符没有做任何事情,所以:

代码语言:javascript
复制
from __future__ import print_function
def printit():
    print ("NUCLEAR CORE UNSTABLE!!!",
           "Quarantine is in effect.",
           "Surrounding hamlets will be evacuated.",
           "Anti-radiationsuits and iodine pills are mandatory.\n",
           sep="\n")

就我个人而言,我更喜欢这个。您可以将所有内容放在一行中,但这会使代码难以阅读和维护。

票数 0
EN

Stack Overflow用户

发布于 2019-06-20 02:42:42

代码语言:javascript
复制
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

print bcolors.WARNING + "Warning: No active frommets remain. Continue?" 
      + bcolors.ENDC

这是我在网上找到的一个代码片段,很好用!

代码语言:javascript
复制
    print bcolors.WARNING + "NUCLEAR CORE UNSTABLE!!!" + bcolors.ENDC + '''\n Quarantine is in effect. \n
Surrounding hamlets will be evacuated. , Anti-radiationsuits and iodine pills are mandatory.'''
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56673964

复制
相关文章

相似问题

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