2 回答
TA贡献1836条经验 获得超5个赞
您使用的是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")
个人比较喜欢这个。您可以将所有内容放在一行中,但这会使代码难以阅读和维护。
TA贡献1934条经验 获得超2个赞
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 放入制表符空间
添加回答
举报