还有另一种方式可以写这个吗?我只知道输入功能,但我似乎无法像示例那样得到它

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (6)

我似乎无法获得与我使用基本打印的示例相同的打印功能,但它不会给我我想要的东西,也是逗号似乎没有分开它

python 2.7

print "NUCLEAR CORE UNSTABLE!!!, Quarantine is in effect. , Surrounding hamlets will be evacuated. , Anti-radiationsuits and iodine pills are mandatory."

提问于
用户回答回答于

您使用的是print 语句,而不是函数,有几种方法可以实现:

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

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

要运行3次:

for i in range(3):
    printit()

这使用多个print语句:

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"

这只使用一行嵌入的换行符:

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

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

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")

我个人更喜欢这个。你可以将它全部放在一行上,但这会使代码难以阅读和维护。

用户回答回答于
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

这是我在网上找到并且有效的代码片段!

    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.'''

您还可以使用\ t来放置选项卡空间

扫码关注云+社区

领取腾讯云代金券