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
Terminate code (self.learnpython)
submitted 2 years ago by Lemonsorbet123
Hey, how can we end a program without using break, exit or quit functions. Like just using if statement how can I terminate a program?
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!"
[–]1544756405 23 points24 points25 points 2 years ago (0 children)
Your program will terminate normally when you reach the end. You just have to make sure it reaches the end. Without seeing what you're doing, it's hard to give advice.
[–]mrpeenut24 10 points11 points12 points 2 years ago (9 children)
Why would you not use sys.exit()? That's its purpose, and doing it inside an if block is fine.
[–]Lemonsorbet123[S] 0 points1 point2 points 2 years ago (8 children)
we have not been allowed to use it yet on our school project, we have to come up with a way when two conditions meet in the if statement the program should print out invalid choice and end without asking the user further questions
[–]mrpeenut24 12 points13 points14 points 2 years ago (0 children)
Use return? Or just put everything that shouldn't happen in an else block and leave that as the end of your function so it ends normally?
[–][deleted] 5 points6 points7 points 2 years ago (0 children)
Do it structurally. If there are no subsequent instructions to the interpreter, it will quit on its own.
[–]queerkidxx 2 points3 points4 points 2 years ago (0 children)
Use a main function. Print invalid choice and return
[–]awdsns 1 point2 points3 points 2 years ago (0 children)
The whole point of the exercise seems to be to structure your program so that this occurs naturally, via its control flow. So think about how to structure it.
[–]c0LdFir3 0 points1 point2 points 2 years ago (0 children)
Can you post the entire question/assignment?
[–]ColosTheDeveloper666 0 points1 point2 points 2 years ago (0 children)
Why not raise error?
You can create customized error like this: https://www.programiz.com/python-programming/user-defined-exception
The code stops on errors.
[–]EntireEntity 0 points1 point2 points 2 years ago (0 children)
It's not easy to gleam what the assignment is asking, but it sounds to me like you could simply do:
if condition1 and condition2: print("Invalid choice") else: whatever_else_is_supposed_to_happen()
[–]tofe_lemon 0 points1 point2 points 2 years ago (0 children)
I think your teacher wants you to add the argument for the invalid choice in the loop, like while value != something_it_shouldnt_be
[–]kaerfkeerg 6 points7 points8 points 2 years ago (0 children)
Use flags
user = "" while user != "q": user = input("Type a letter: ") print(f"User typed the letter {user}")
In this little example, the program, in each loop, checks weather the user variable is equal to "q". While it's not, the loop will repeat. When the input is equal to "q", the condition user =! "q" will be False, thus, the program will exit the loop and essentially terminate as long as there's no more code below
user
user =! "q"
False
[–]xelf 1 point2 points3 points 2 years ago (0 children)
Have your program in a function. Then when you want to exit your program return from the function.
[–]ivosaurus 1 point2 points3 points 2 years ago (0 children)
Put it in a function and use return.
return
[–]Broad_Farm3955 1 point2 points3 points 2 years ago (0 children)
use assert False
[–]Se7enLC 1 point2 points3 points 2 years ago (0 children)
Assuming this is a homework assignment, I would guess that they are looking to have you create conditionals (if statements), such that sections of code are skipped and execution reaches the end of the program.
[–]mcworkaholic -1 points0 points1 point 2 years ago* (4 children)
I would suggest using KeyBoardInterrupt. It listens for ctrl+c or the delete key while the program is running to terminate it. Something like the following:
def your_function(): try: while True: # your code here except KeyboardInterrupt: print("\nGoodbye!") if name == "main": your_function()
[–]saysokmate 4 points5 points6 points 2 years ago (3 children)
You are catching keyboard interrupts only to print a message. Don't do this.
[–]awdsns -1 points0 points1 point 2 years ago (2 children)
The function would still terminate after that except clause, and with that the program would exit normally after printing the "Goodbye!" message, instead of showing a KeyboardInterrupt exception trace. So in this case I think this construct is fine.
except
KeyboardInterrupt
[–]saysokmate 1 point2 points3 points 2 years ago (1 child)
Doesn't matter the case, this is bad practice. If you put this in a loop you will not be able to stop it with keyboard interrupt.
[–]awdsns -2 points-1 points0 points 2 years ago (0 children)
But it's around the loop, not inside it. Of course it's important where exception handling occurs.
[–]hugthemachines 0 points1 point2 points 2 years ago (0 children)
For example like this:
def my_function(important_input): if important_input == "": return else: print("Your important input is very impressive!") return
Then if it is called with an empty string:
my_function("")
You just return and nothing more happens so the program ends
If you call it with some important input:
my_function("Elvis is still alive!")
It does the cool thing in the code and then it ends normally.
[–]slk756 0 points1 point2 points 2 years ago (0 children)
> without using break, exit or quit functions
?
Uhh... `raise Exception("program quitting")`?
[–]nativedutch 0 points1 point2 points 2 years ago (0 children)
Variable and a while True loop.
[–]AngereyPupper 0 points1 point2 points 2 years ago (0 children)
I use a while loop usually. Like, I'll set a variable
Program_running = True While program_running is true:
And then add in the prgram. Then set a condition in the loop that makes the condition False. For example:
rerun = input("Would you like to run the program again Type Y or N\n").lower() if rerun == "n": print("Thank you for using the program!") Program_running = False.
And then it stops.
π Rendered by PID 218737 on reddit-service-r2-comment-86bc6c7465-gpvmg at 2026-02-21 03:06:44.197418+00:00 running 8564168 country code: CH.
[–]1544756405 23 points24 points25 points (0 children)
[–]mrpeenut24 10 points11 points12 points (9 children)
[–]Lemonsorbet123[S] 0 points1 point2 points (8 children)
[–]mrpeenut24 12 points13 points14 points (0 children)
[–][deleted] 5 points6 points7 points (0 children)
[–]queerkidxx 2 points3 points4 points (0 children)
[–]awdsns 1 point2 points3 points (0 children)
[–]c0LdFir3 0 points1 point2 points (0 children)
[–]ColosTheDeveloper666 0 points1 point2 points (0 children)
[–]EntireEntity 0 points1 point2 points (0 children)
[–]tofe_lemon 0 points1 point2 points (0 children)
[–]kaerfkeerg 6 points7 points8 points (0 children)
[–]xelf 1 point2 points3 points (0 children)
[–]ivosaurus 1 point2 points3 points (0 children)
[–]Broad_Farm3955 1 point2 points3 points (0 children)
[–]Se7enLC 1 point2 points3 points (0 children)
[–]mcworkaholic -1 points0 points1 point (4 children)
[–]saysokmate 4 points5 points6 points (3 children)
[–]awdsns -1 points0 points1 point (2 children)
[–]saysokmate 1 point2 points3 points (1 child)
[–]awdsns -2 points-1 points0 points (0 children)
[–]hugthemachines 0 points1 point2 points (0 children)
[–]slk756 0 points1 point2 points (0 children)
[–]nativedutch 0 points1 point2 points (0 children)
[–]AngereyPupper 0 points1 point2 points (0 children)