2017-03-10 19 views
2

これは初心者の質問です。私はテキストゲームを書いていて、いくつか問題があります。Python 2.7.13複数行印刷

コード:

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

私はこれらのprint文を好きではありません。選択肢1のように複数行の印刷を使用すると、インデントがうまく見えません。それはdef river()と同じ列にあります。これは普通か悪いか?選択肢Iの下にテキストをインデントすると、選択肢1と同じ行にあります。コマンドプロンプトでスクリプトを開始すると、うまく見えません。どうすればそれを解決できますか?

おかげ

+0

'print'文の中で' \ n'を使わないのはなぜですか? –

答えて

2

私はこのようなものを記述します。暗黙的に文字列を続ける

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が関数であるため、そこには空白がありません。