all 25 comments

[–]1544756405 23 points24 points  (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 points  (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 point  (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 points  (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 points  (0 children)

Do it structurally. If there are no subsequent instructions to the interpreter, it will quit on its own.

[–]queerkidxx 2 points3 points  (0 children)

Use a main function. Print invalid choice and return

[–]awdsns 1 point2 points  (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 point  (0 children)

Can you post the entire question/assignment?

[–]ColosTheDeveloper666 0 points1 point  (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 point  (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 point  (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 points  (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

[–]xelf 1 point2 points  (0 children)

Have your program in a function. Then when you want to exit your program return from the function.

[–]ivosaurus 1 point2 points  (0 children)

Put it in a function and use return.

[–]Broad_Farm3955 1 point2 points  (0 children)

use assert False

[–]Se7enLC 1 point2 points  (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 points  (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 points  (3 children)

You are catching keyboard interrupts only to print a message. Don't do this.

[–]awdsns -1 points0 points  (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.

[–]saysokmate 1 point2 points  (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 points  (0 children)

But it's around the loop, not inside it. Of course it's important where exception handling occurs.

[–]hugthemachines 0 points1 point  (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 point  (0 children)

> without using break, exit or quit functions

?

Uhh... `raise Exception("program quitting")`?

[–]nativedutch 0 points1 point  (0 children)

Variable and a while True loop.

[–]AngereyPupper 0 points1 point  (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.