python raw_input分开的行

时间:2015-08-19 17:54:54

标签: python lines raw-input

这段代码在编辑器中太长了,需要我滚动才能看到它。如何将代码分解为多行?

review = raw_input('If the secret number is too high enter h, if the secret  number is too low enter l and if it is correct enter c: ')

2 个答案:

答案 0 :(得分:1)

你可以将字符串分开并将它们各自放在一行 -

review = raw_input('If the secret number is too high enter h'
                   ', if the secret  number is too low enter l'
                   'and if it is correct enter c: ')

示例/演示 -

>>> review = raw_input('If the secret number is too high enter h'
...                    ', if the secret  number is too low enter l'
...                    'and if it is correct enter c: ')
If the secret number is too high enter h, if the secret  number is too low enter land if it is correct enter c: h
>>> review
'h'

要在多行上打印,请使用"""'''(三个引号)创建多行字符串 -

s = '''If the secret number is too high enter h
, if the secret  number is too low enter l
and if it is correct enter c: '''

review = raw_input(s)

示例/演示 -

>>> s = '''If the secret number is too high enter h
... , if the secret  number is too low enter l
... and if it is correct enter c: '''
>>>
>>> review = raw_input(s)
If the secret number is too high enter h
, if the secret  number is too low enter l
and if it is correct enter c: c
>>> review
'c'

为了便于阅读,我使用了一个单独的字符串,但您可以直接给raw_input()一个多行字符串,而不必将其存储在任何变量中。

答案 1 :(得分:0)

review = raw_input("""If the secret number is too high enter h,  
                   if the secret  number is too low enter l 
                   and if it is correct enter c: """)

对多行字符串使用三引号。