这是一个开始的问题。我正在写文字游戏,但有一些问题。

码:

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


我不喜欢这些印刷声明。如果我使用多行打印(如选择1),则缩进效果不太好。它与def river()在同一行。这是正常样式还是不良样式?如果我缩进正在选择的文本,那么它与选择1在同一行。..那么当我在命令promt中启动脚本时,它看起来不好。我该如何解决?

谢谢

最佳答案

我会这样写:

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 Python docs。例如:

print ("foo "
       "bar")


print "foo bar"

将给出相同的输出。注意“ foo”之后的空格。

请注意,在print语句和第一个括号之间有一个空格。在python3中,print是一个函数,那里应该没有空格。

关于python - Python 2.7.13多行打印,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42718403/

10-09 09:35