我似乎无法获得与示例相同的打印功能 我已经使用了基本的印刷品,但是它不会给我我想要的东西,而且逗号似乎也没有将其分开
python 2.7
print "NUCLEAR CORE UNSTABLE!!!, Quarantine is in effect. , Surrounding hamlets will be evacuated. , Anti-radiationsuits and iodine pills are mandatory."
答案 0 :(得分:0)
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放置制表符空间
答案 1 :(得分:0)
您使用的是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
function ,并抱怨逗号分隔符什么也没做,所以:
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")
我个人更喜欢这个。您可以将所有内容放在一起,但这会使代码难以阅读和维护。