简体   繁体   English

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

[英]is there another way i can write this? i only know the print function,but i can not seem to get it like the example

i can not seem to get the same print function as the example i have used the basic print but it wont give me what i am looking for, also the commas don not seem to divide it 我似乎无法获得与我使用基本打印的示例相同的打印功能,但它不会给我我想要的东西,而且逗号似乎也没有分开它

python 2.7 python 2.7

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

在此输入图像描述

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

This is a code snippet that I found online and works! 这是我在网上找到并且有效的代码片段!

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

You can also use \\t to put in a tab space 您还可以使用\\ t来放置选项卡空间

You are using the print statement , not function, and there are several ways to do it: 您正在使用print 语句 ,而不是函数,有几种方法可以实现:

This uses a triple quoted string to preserve newlines: 这使用三引号字符串来保留换行符:

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

To run it 3 times just: 要运行3次:

for i in range(3):
    printit()

This uses multiple print statements: 这使用多个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"

This uses just one line with embedded newlines: 这只使用一行嵌入的换行符:

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

However, you mentioned print function and complained about the comma separators not doing anything, so: 但是,你提到了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")

Personally I prefer this one. 我个人更喜欢这个。 You could put it all on one line, but that makes the code difficult to read and maintain. 你可以将它全部放在一行上,但这会使代码难以阅读和维护。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能像这个例子一样打印这些行? - How can i print the lines like this example? 有没有另一种方法可以做到这一点? 我已经尝试了 6 个版本的代码,运行时似乎无法得到结果 - is there another way to make this ? I have tried 6 versions of code, I can not seem to get a result when I run it 我有办法重写代码吗?,我似乎无法正确执行,我似乎无法正确获取输入 - i there a way to rewrite the code?,i can not seem to get it right, i can not seem to get the input correctly 我似乎无法准确地得到它,我正在得到阶乘,但似乎无法找到一种方法来放入组合公式 - i can not seem to get it exactly, i am getting the factorials but cant seem to find a way to put in the combination formula 我似乎无法使用此功能将列表添加在一起 - I can't seem to get this function to add the list together 仅当使用 print 调用时,我才能将函数打印到 shell 中吗? - Can I make a function print into shell only if called with print? 我可以从另一个.py 文件中的一个 function 打印一个变量吗 - Can I print a variable from one function in another .py file 如何在python中打印characters等字符 - How can I print characters like ♟ in python 我如何打印像这样的输出列表 - how can I print list like this output 我怎样才能将解决方案打印成分数,而不是那样? - How can I print the solutions as a fraction, not like that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM