-4

Just want to ask a simple question.

Because I tried to write the following code into single line,

  • Before:
while True: 
    print('Hello world')
    break
  • After:
while True: print('Hello world') break
  • Error message:
File "<ipython-input-19-cd7b3be1f22e>", line 1
    while True: print('Hello world') break
                                         ^
SyntaxError: invalid syntax
  • Have tried:
while True: print('Hello world')
    break
  • Error message again:
File "<ipython-input-20-0ecfc981712d>", line 2
    break
    ^
IndentationError: unexpected indent

Is any idea to give "break" properly if I want to use it to stop while loop within single line?

Thanks in advance.

Fatlip
  • 11
  • 1
    You can put it in a single line with `print('Hello world')`, which does the same thing. `break` is only useful if done conditionally. – interjay Nov 28 '20 at 09:52
  • 1
    Python is not designed to facilitate shoving all possible programs onto a single line. – user2357112 Nov 28 '20 at 09:54
  • @user2357112supportsMonica True, in fact Python's philosophy is code readability, and people should avoid minified JavaScript like the monster that is minified jQuery –  Nov 28 '20 at 09:56

4 Answers4

0

try this code

while True: print('Hello world'); break
sourab maity
  • 1,025
  • 2
  • 8
  • 16
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Nov 28 '20 at 10:20
  • Thanks for reminder:) There is no doubt that indentation is important to separate different code blocks in Python (such as conditional expressions, for loop, while loop...). That's the reason why I think I can purely use indentation to separate "break" from another parts of while loop. It seems that semicolon in Python still works in some cases though it's useless in most of time. Also, that would be the feature of Python comparing with other languages likeJavaScript or C. – Fatlip Nov 28 '20 at 10:50
  • Thanks if you want you can edit my ans – sourab maity Nov 28 '20 at 10:53
  • English is not so fluent for me so i add small English – sourab maity Nov 28 '20 at 10:55
0

You can basically use ; in one line statments

while True: 
    print('Hello world')
    break

Basically this is equivalent

while True: print('Hello world'); break
cangokceaslan
  • 467
  • 1
  • 5
  • 12
0

You can use a semicolon.(;)

Like this:

while True: print('Hello world'); break

but it's not that good idea. since Python uses indentation to mark blocks.

ppwater
  • 2,315
  • 4
  • 15
  • 29
  • Yes, you are absolutely right. I think that the reason why I've rarely seen semicolons in Python cause the function of them have been replaced by indentation. It's good to know this. Thanks for recommendations:) – Fatlip Nov 28 '20 at 10:25
0

Just like in languages like JavaScript, Java and C, you can write:

while True: print('Hello World'); break;

using semicolons to separate statements