Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

Python 3, 29 bytes

 or~print(0)
while 1:print(1)

A full program, which if prefixed with 0 will print 0 then halt, exiting with an error, or if prefixed with 1 will print 1 forever.

Try 0 online!
Try 1 online!

The right of a logical or is only executed if the left is falsey (which 0 is, while 1 is not).

If the prefix is 0 the argument, print(0), of bitwise-not, ~, is evaluated - this has a side-effect of printing 0 and returns None which is an invalid argument for ~ causing an error which halts the program.

If the prefix is 1 the code ~print(0) is not evaluated and the infinite loop of while 1:print(1) is reached.

Python 3, 29 bytes

 or~print(0)
while 1:print(1)

A full program, which if prefixed with 0 will print 0 then halt, exiting with an error, or if prefixed with 1 will print 1 forever.

Try 0 online!
Try 1 online!

The right of a logical or is only executed if the left is falsey (which 0 is, while 1 is not).

If the prefix is 0 the argument, print(0), of bitwise-not, ~, is evaluated - this has a side-effect of printing 0 and returns None which is an invalid argument for ~ causing an error which halts the program.

If the prefix is 1 the code ~print(0) is not evaluated and the infinite loop of while 1:print(1) is reached.

Python 3, 29 bytes

 or~print(0)
while 1:print(1)

A full program, which if prefixed with 0 will print 0 then halt, exiting with an error, or if prefixed with 1 will print 1 forever.

Try 0 online!
Try 1 online!

The right of a logical or is only executed if the left is falsey (which 0 is, while 1 is not).

If the prefix is 0 the argument, print(0), of bitwise-not, ~, is evaluated - this has a side-effect of printing 0 and returns None which is an invalid argument for ~ causing an error which halts the program.

If the prefix is 1 the code ~print(0) is not evaluated and the infinite loop of while 1:print(1) is reached.

deleted 33 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Python 3, 29 bytes

 or~print(0)
while 1:print(1)

A full program, which if prefixed with 0 will print 0 then halt, exiting with an error, or if prefixed with 1 will print 1 forever.

Try 0 online!
Try 1 online!

The right of a logical or is only executed if the left is falsey (which 0 is, while 1 is not).

If the prefix is 0 the argument, (print(0)), of bitwise-not, ~, is evaluated - this has a side-effect of printing 0 and returns None, which is an invalid argument for ~ causing an error which halts the program.

If the prefix is 01 the code ~print(0) is not evaluated (since 1 or x is always True) and and the infinite loop of while 1:print(1) is reached.

Python 3, 29 bytes

 or~print(0)
while 1:print(1)

A full program, which if prefixed with 0 will print 0 then halt, exiting with an error, or if prefixed with 1 will print 1 forever.

Try 0 online!
Try 1 online!

The right of a logical or is only executed if the left is falsey (which 0 is, while 1 is not).

If the prefix is 0 the argument (print(0)) of bitwise-not, ~ is evaluated - this has a side-effect of printing 0 and returns None, which is an invalid argument for ~ causing an error which halts the program.

If the prefix is 0 the code ~print(0) is not evaluated (since 1 or x is always True) and the infinite loop of while 1:print(1) is reached.

Python 3, 29 bytes

 or~print(0)
while 1:print(1)

A full program, which if prefixed with 0 will print 0 then halt, exiting with an error, or if prefixed with 1 will print 1 forever.

Try 0 online!
Try 1 online!

The right of a logical or is only executed if the left is falsey (which 0 is, while 1 is not).

If the prefix is 0 the argument, print(0), of bitwise-not, ~, is evaluated - this has a side-effect of printing 0 and returns None which is an invalid argument for ~ causing an error which halts the program.

If the prefix is 1 the code ~print(0) is not evaluated and the infinite loop of while 1:print(1) is reached.

added 249 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Python 3, 29 bytes

 or~print(0)
while 1:print(1)

A full program, which if prefixed with 0 will print 0 then halt, exiting with an error, or if prefixed with 1 will print 1 forever.

Try 0 online!
Try 1 online!

Note that the resultThe right of callinga logical or is only executed if the left is falsey (which 0 is, while 1 is not).

If the prefix is 0 the argument (print(0) function) of bitwise-not, ~ is evaluated - this has a side-effect of printing None0, and thatreturns None, which is an invalid argument for bitwise-not, ~ causing an error which halts the program. Furthermore

If the right of a logicalprefix is or0 is only executed if the leftcode ~print(0) is falseynot evaluated (whichsince 01 or x is, while always True) and the infinite loop of while 1:print(1) is not)reached.

Python 3, 29 bytes

 or~print(0)
while 1:print(1)

A full program, which if prefixed with 0 will print 0 then halt, exiting with an error, or if prefixed with 1 will print 1 forever.

Try 0 online!
Try 1 online!

Note that the result of calling the print function is a None, and that None is an invalid argument for bitwise-not, ~. Furthermore the right of a logical or is only executed if the left is falsey (which 0 is, while 1 is not).

Python 3, 29 bytes

 or~print(0)
while 1:print(1)

A full program, which if prefixed with 0 will print 0 then halt, exiting with an error, or if prefixed with 1 will print 1 forever.

Try 0 online!
Try 1 online!

The right of a logical or is only executed if the left is falsey (which 0 is, while 1 is not).

If the prefix is 0 the argument (print(0)) of bitwise-not, ~ is evaluated - this has a side-effect of printing 0 and returns None, which is an invalid argument for ~ causing an error which halts the program.

If the prefix is 0 the code ~print(0) is not evaluated (since 1 or x is always True) and the infinite loop of while 1:print(1) is reached.

Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293
Loading

AltStyle によって変換されたページ (->オリジナル) /