python raw_input separate lines
Asked Answered
O

2

0

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: ')
Origami answered 19/8, 2015 at 17:54 Comment(1)
\ ..................Subclavian
A
1

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

Example/Demo -

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

Example/Demo -

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

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.

Availability answered 19/8, 2015 at 17:56 Comment(3)
Thank you very much. I assume that doesn't change the fact that when it prints out it's still on one line. Out of curiosity is there a way to print it out on multiple lines?Origami
@AnandSKumar I hope you're not mad for editing. It's not important for me to score.already I'm older I'm going to die. Best Regards..Gollin
I am not mad, But truly I do believe those kinds of edits should go to comments.Availability
M
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: """)

Use triple quotes for multi-line string.

Moderator answered 19/8, 2015 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.