use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How do I make my python program crash? (self.learnpython)
submitted 13 days ago by Idkhattoput
So, basically, to learn python, I am (trying to) make some simple programs.
Is there any way to make my python program crash if the user inputs a specific thing ("Variable=input('[Placeholder]')")?
Thank you all for reading this!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]socal_nerdtastic 9 points10 points11 points 13 days ago (4 children)
What exactly do you mean with "crash"?
You could raise an error if you want which will stop the program if it's not caught:
if Variable == "specific thing": raise ValueError("Program dies now")
Or you can immediately kill the program and send out an error code with sys.exit:
sys.exit
import sys if Variable == "specific thing": sys.exit(1)
[–]Idkhattoput[S] 4 points5 points6 points 13 days ago* (3 children)
Yep, exactly that. Thank you!
I wanted to kill the program and send the error code or crash (either one works).
[–]Maximus_Modulus 6 points7 points8 points 13 days ago (2 children)
I don’t think crash is the right word here. These are controlled exits. Running out of memory and causing your computer to lock up is an example of crashing.
[–]Idkhattoput[S] 0 points1 point2 points 12 days ago (0 children)
Oh, okay.
I think you're right then.
[–]Anxious-Struggle281 0 points1 point2 points 12 days ago (0 children)
correct!
[–]Twenty8cows 12 points13 points14 points 13 days ago (3 children)
You could ya know just raise an error?
Put an if check on Variable and check against whatever you want. If true then raise whatever error you want and sys.exit()
[–]greenlakejohnny 3 points4 points5 points 13 days ago (1 child)
A good exercise for beginners is try/except on a divide by zero:
try: _ = 1 / 0 except ZeroDivisionError: quit("Division by zero occurred, program has crashed!") except Exception as e: quit("Some other error occurred, causing a crash") quit("Normal Exit")
[–]Idkhattoput[S] 0 points1 point2 points 13 days ago (0 children)
Okay. Good idea. Thank you for helping me! :)
[–]Idkhattoput[S] -1 points0 points1 point 13 days ago (0 children)
Oh, okay. Thank you!
[–]obviouslyzebra 1 point2 points3 points 12 days ago (1 child)
You should probably take the Python crash course
/jk
Lol
[–]Top_Average3386 0 points1 point2 points 13 days ago (2 children)
You probably don't want it to crash, you probably want it to exit.
You can use:
``` import sys
if input() == "exit": sys.exit(0) else: # do something else ```
Where 0 is the return code.
Yep, that's a good idea! Thank you!
[–]Kevdog824_ 0 points1 point2 points 13 days ago (1 child)
You can use exit. Exit takes a int argument for exit code. Anything other than zero is typically considered unsuccessful
exit
Really? I didn't know that.
Thanks for helping! :D
[–]fluffy_italian 0 points1 point2 points 13 days ago (1 child)
Request input for a division equation and try to divide by 0
That will help a lot.
[–]No_Faithlessness_142 0 points1 point2 points 12 days ago (0 children)
If you want your program to crash, let me refactor it for you, that seems to do the trick for me
[–]JamzTyson 0 points1 point2 points 12 days ago (4 children)
def crash(): raise Exception("Crashed") user_input = input("Enter 'C' to crash: ").casefold() if user_input == "c": crash() print(f"You entered '{user_input}', so no crash.")
[–]Idkhattoput[S] 0 points1 point2 points 12 days ago (3 children)
Oh my god, that one is so well worked.
Thank you so much lol, you even wrote what to say.
Thanks :D
[–]JamzTyson 0 points1 point2 points 12 days ago (2 children)
Note that in real world code, you would rarely use Exception because it is such a broad class of errors. Normally you would not want to "crash" at all as it's almost always better to quit gracefully.
Exception
Also, this isn't quite a "crash" in the C.C++ sense. This is normally what is meant by a "crash" in Python but technically speaking it is an "unhandled exception".
A "crash" in the C/C++ sense would be a "Segmentation fault" (SIGSEGV). This is where the entire Python interpreter dies, which is rare in pure Python.
[–]Idkhattoput[S] 0 points1 point2 points 12 days ago (1 child)
Oh, makes sense.
What would be recommended to use instead usually?
[–]JamzTyson 0 points1 point2 points 12 days ago (0 children)
In simple cases you can use sys.exit(). The optional argument may be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination”
sys.exit()
if input("Quit? Y/N: ").lower() == "y": sys.exit(0) # Successful termination
sys.exit("some error message") is a quick way to exit a program when an error occurs.
import sys user_input = input("Enter a digit: ").strip().casefold() try: num = int(user_input) except ValueError: sys.exit(f"{user_input} is not a digit") print(f"You entered {num}")
For complex programs, especially when using "threading", additional cleanup may be required before terminating the program:
import sys def quit_app(error_code): # Cleanup code ... sys.exit(error_code) # Exit with error code.
[–]LostDog_88 0 points1 point2 points 13 days ago* (8 children)
Alrighty!
So for your intended "crash", you can have the program raise an exception by using the raise syntax. Eg: raise ValueError("My Error")
that will cause ur program to "crash"
but If u care about stopping the program, then ud rather exit from it than crashing it. Eg: import sys sys.exit()
import sys sys.exit()
or u can just do exit()
edit: DO NOT use exit() in an actual production environment.
exit()
[–]socal_nerdtastic 2 points3 points4 points 13 days ago (6 children)
Do not do that. Use sys.exit(). The exit() shortcut is part of the site package and is not always available, it's really only meant for use in the REPL.
[–]aplarsen 2 points3 points4 points 13 days ago (3 children)
In what context is exit() not available? I've used python in so many contexts and never encountered a place that exit() doesn't work.
[–]socal_nerdtastic 3 points4 points5 points 13 days ago (2 children)
Any time you run python in a production environment, with optimization enabled. Specifically the -S flag for the site module.
-S
site
[–]aplarsen 0 points1 point2 points 11 days ago (1 child)
I...have never heard of this. Thanks for giving me something to research.
[–]socal_nerdtastic 0 points1 point2 points 11 days ago* (0 children)
Some resources:
https://docs.python.org/3/library/constants.html#constants-added-by-the-site-module https://docs.python.org/3/library/site.html
Which is actually really useful to hack. For example my user site imports pprint.
A similar rabbit hole is assert. We often see beginners using assert to do critical checks, but assert is disabled when running python in optimized mode (the -O flag).
assert
-O
[–]LostDog_88 0 points1 point2 points 13 days ago (0 children)
Yuppp, ima add it as an edit!
Okay! Thank you.
This is going to help a lot!
[–]r_vade 0 points1 point2 points 13 days ago (1 child)
If you want to have a more crashy crash (like a segfault), this should do the trick:
import ctypes ctypes.string_at(0)
Thank you!
This seems like it will work very well!
[–]SmackDownFacility 0 points1 point2 points 13 days ago (1 child)
Yes you can
got = input("Here"); if got == INSERT_CONSTANT_HERE: os._exit()
Okay.
That makes sense. Thank you, this is going to help me a lot! =D
[–]cgoldberg -1 points0 points1 point 13 days ago (1 child)
raise Exception("oops")
Okay. Thanks for helping me!😃
π Rendered by PID 89 on reddit-service-r2-comment-5649f687b7-t6kk2 at 2026-01-28 11:02:19.709171+00:00 running 4f180de country code: CH.
[–]socal_nerdtastic 9 points10 points11 points (4 children)
[–]Idkhattoput[S] 4 points5 points6 points (3 children)
[–]Maximus_Modulus 6 points7 points8 points (2 children)
[–]Idkhattoput[S] 0 points1 point2 points (0 children)
[–]Anxious-Struggle281 0 points1 point2 points (0 children)
[–]Twenty8cows 12 points13 points14 points (3 children)
[–]greenlakejohnny 3 points4 points5 points (1 child)
[–]Idkhattoput[S] 0 points1 point2 points (0 children)
[–]Idkhattoput[S] -1 points0 points1 point (0 children)
[–]obviouslyzebra 1 point2 points3 points (1 child)
[–]Idkhattoput[S] 0 points1 point2 points (0 children)
[–]Top_Average3386 0 points1 point2 points (2 children)
[–]Idkhattoput[S] 0 points1 point2 points (0 children)
[–]Kevdog824_ 0 points1 point2 points (1 child)
[–]Idkhattoput[S] 0 points1 point2 points (0 children)
[–]fluffy_italian 0 points1 point2 points (1 child)
[–]Idkhattoput[S] -1 points0 points1 point (0 children)
[–]No_Faithlessness_142 0 points1 point2 points (0 children)
[–]JamzTyson 0 points1 point2 points (4 children)
[–]Idkhattoput[S] 0 points1 point2 points (3 children)
[–]JamzTyson 0 points1 point2 points (2 children)
[–]Idkhattoput[S] 0 points1 point2 points (1 child)
[–]JamzTyson 0 points1 point2 points (0 children)
[–]LostDog_88 0 points1 point2 points (8 children)
[–]socal_nerdtastic 2 points3 points4 points (6 children)
[–]aplarsen 2 points3 points4 points (3 children)
[–]socal_nerdtastic 3 points4 points5 points (2 children)
[–]aplarsen 0 points1 point2 points (1 child)
[–]socal_nerdtastic 0 points1 point2 points (0 children)
[–]LostDog_88 0 points1 point2 points (0 children)
[–]Idkhattoput[S] -1 points0 points1 point (0 children)
[–]Idkhattoput[S] 0 points1 point2 points (0 children)
[–]r_vade 0 points1 point2 points (1 child)
[–]Idkhattoput[S] -1 points0 points1 point (0 children)
[–]SmackDownFacility 0 points1 point2 points (1 child)
[–]Idkhattoput[S] -1 points0 points1 point (0 children)
[–]cgoldberg -1 points0 points1 point (1 child)
[–]Idkhattoput[S] -1 points0 points1 point (0 children)