2017-03-10 2 views
2

이것은 시작적인 질문입니다. 나는 텍스트 게임을 쓰고 있는데 문제가있다.Python 2.7.13 multiline print

코드 :

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과 같은 행에 있습니다. 그러면 명령 프롬프트에서 스크립트를 시작할 때 모양이 좋지 않습니다. 내가 어떻게 해결할 수 있니?

감사

+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은 함수이고, 거기에는 공백이 없어야합니다.