Exit("") Not Working Properly in Python

This one is slightly different than @CoderElijah’s :slight_smile:

Normally, when you use exit(“”) it would show whatever you put into in red, and then say “Exit status 1” however, that is not the case in my Repl. When I use exit(“”) in my code:

else:
  print("Sorry, but the number you entered is not a valid option." + "\n" + "Please try again." + "\n")
  exit("Exiting...")

It just stops running, without making it red and without the “Exit status 1” message.

image

Even if you add exit("\033[31mExiting...\nexit status 1\033[37m"), it looks like this in the IDE (which is correct):

image

But then if someone runs it on the cover page, it looks like this:

image

Here is my Repl btw:

Cyber Tools - Replit

My conclusion is as you described it. With the text “Exiting…” and the red line “exit status 1”

image

Why not use ANSI to make it say that?

exit("Exiting...\n\033[31mexit status 1\033[37m")

This displays your message, then exit status 1 in red, then restores the color to white just in case.

Huh so maybe it just does that in the IDE? But even still, in your screenshot the “Exiting…” isn’t red even though it should be.

Make Exiting... red using ANSI too.

exit("\033[31mExiting...\nexit status 1\033[37m")

Yeah that works for now, but the bug is still there :slight_smile:

My guess is that this isn’t a bug so much as a feature in the new Python template. Kinda annoying to not be able to turn off that message. Now you can. :slight_smile:

1 Like

Well the problem is that with your code, it looks like this in the IDE (which is correct):

image

But then if someone runs it on the cover page, it looks like this:

image

Then check to see if the repl is a ghost fork or not and create a simple if/else statement to adjust accordingly.

@JayAySeaOhBee14 try this code:

from os import environ as e
if e["REPL_ID"] != "349e450e-f936-4e7c-8b95-dc94bf67660a":
  exit("\033[31mExiting...\033[37m")
else:
  exit("\033[31mExiting...\nexit status 1\033[37m")

Works for me. :slight_smile:
Of course replace that repl ID with the ID of your repl (run echo $REPL_ID in Shell to obtain).
You can probably leave out the \033[37m part but I included it just in case (might come in handy if you’re using prybar :man_shrugging: ). It makes the text white again.

@CoderElijah Or you could use @bigminiboss’ code for a cover page check (Repl can be found here):

import uuid
import os

def is_cover_page():
    x = os.environ["REPL_ID"]
    return not (str(uuid.UUID(x, version=4)) == x)

if is_cover_page():
  exit("\033[31mExiting...\033[37m")
else:
  exit("\033[31mExiting...\nexit status 1\033[37m")
For the sake of minification:
from os import environ as e

is_cover_page = lambda: not (str(__import__("uuid").UUID(e["REPL_ID"], version=4)) == e["REPL_ID"])

if is_cover_page():
  exit("\033[31mExiting...\033[37m")
else:
  exit("\033[31mExiting...\nexit status 1\033[37m")
3 Likes

I couldn’t remember where that repl was. Also that link is missing the repl name. Mine only uses one import. :slight_smile:

Don’t know how that happened, fixing now.

I think they’re both builtin modules.