简体   繁体   English

Python 2.7.13多行打印

[英]Python 2.7.13 multiline print

This is a begginer question. 这是一个初学者的问题。 I am writing a text game and have some problems. 我写一个文字游戏,有一些问题。

code: 码:

def river():
    print "You are traveling through deciduous forest."
    print "There is nothing going on, except a few squirrels and a hedge."
    print "Squirrels are fighting over nuts and a hedge is getting fat eating an apple."
    print "Then you see a wide, deep river. The stream is very strong."
    print "What are you going to do next?"
    print "1. Try to swim the river."
    print "2. Watch the squirrels and hedge."
    print "3. Turn back."

    while True:
        choice = raw_input("> ")

        if choice == "1" or choice == "swim":
            print """You are doing well when in the middle of a river
something begins to bite you near legs. The bitting gets stronger
and you soon realize that piranhas are tearing chunks of your meat
off. Soon there is nothing left of you that the bones. The piranhas
seems satisfied, since they got a free meal."""
            dead()
        if choice == "2" or choice == "watch":
            print "You keep watching animals and are becoming more and more thirsty and hungry."
        if choice == "3" or choice == "turn back" or choice == "turn":
            start_rewind()

start()

I do not like these print statements. 我不喜欢这些打印语句。 If I use multiline print, like under choice 1, the indentation does not look nice. 如果我使用多行打印(如选择1),则缩进效果不太好。 It is in the same row as def river(). 这是同一行DEF河()。 Is this a normal or a bad style? 这是一个正常的或坏的风格? If i indent the text under choice I so it is in the same row as choice 1,.. then it does not look good when i start the script in command promt. 如果我选择下缩进文本我所以它是在同一行作为首选1,..然后它不会当我开始在命令PROMT剧本好看。 How could i solve that? 我该如何解决?

Thanks 谢谢

I would write something like this: 我会这样写:

print ("You are traveling through deciduous forest.\n"
       "There is nothing going on, except a few squirrels and a hedge.\n"
       "Squirrels are fighting over nuts and a hedge is getting fat eating an apple.\n"
       "Then you see a wide, deep river. The stream is very strong.")

The parentheses above implicitly continue the string. 上面的括号隐式地延续字符串。 See the Python docs . 请参阅Python文档 For example: 例如:

print ("foo "
       "bar")

and print "foo bar" print "foo bar"

will give the same output. 将给出相同的输出。 Note the space after "foo". 注意“ foo”之后的空格。

Note that there is a space between the print statement and the first parenthesis. 请注意, print语句和第一个括号之间有一个空格。 In python3, where print is a function, there should be no space there. 在python3中,其中print是一个函数,那里应该没有空格。

try this: 尝试这个:

print("""
You are traveling through deciduous forest.
There is nothing going on, except a few squirrels and a hedge.
Squirrels are fighting over nuts and a hedge is getting fat eating an apple.
Then you see a wide, deep river. The stream is very strong.
What are you going to do next?
1. Try to swim the river.
2. Watch the squirrels and hedge.
3. Turn back.
""")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM