本文介绍了python raw_input单独的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This piece of code is too long in the editor and requires me to scroll to see it. How can I break the code down into multiple lines?

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: ')

推荐答案

您可以将字符串分开,然后将每个字符串放在自己的一行中-

You can divide the strings up and put each of them in a line of its own -

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'


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


To print out on multiple lines, create multiline string, using """ or ''' (three quotes) -

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()一个多行字符串,而不必将其存储在任何变量中.

I used a separate string just for readability, but you can directly give raw_input() a multiline string, without having to store it in any variable.

这篇关于python raw_input单独的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-16 07:42