Using quit or exit in Python? [duplicate] - python

This question already has answers here:
How do I terminate a script?
(14 answers)
Closed 2 years ago.
I was told in another language that it is bad practice to call the equivalent to quit() or exit() in Python.
Rather then calling a similar function to quit() or exit() in Python that it is better to let the program end "naturally". In that the main document ends after completing any function/method calls or any condition checking, etc...
Is this also true in Python or is it fine to use these functions for ending the program?
Edit: I will include a link to my actual code to hopefully add some clarification, it is the bottom two function that I'm having issues with
https://github.com/itwasntzak/delivery_tracking-python/blob/test/menus.py

I have never come across where there is a need for a hard stop like that. You are right that it is common practice to let the program execute in full. If you need to break out of a loop then you can use break. Is there a specific example where you think you need to use a quit() tag?

Related

Is there a way to see the order in which lines are executed in PyCharm? [duplicate]

This question already has answers here:
How to step through Python code to help debug issues?
(15 answers)
Closed 1 year ago.
I am looking at a body of code and am trying to learn how to tell the order that each line executes. Is there a way to get this from a command in Python? Or is there a way to find out the order that Python executes code?
You can step through the program to see which line is being executed when. It's not really possible to say "this line executes after this one" because the code branches every time there is a conditional statement and the execution order changes.
You can step through the code to see which order the program executes lines for one specific set of conditions, though.
See https://www.jetbrains.com/help/pycharm/stepping-through-the-program.html
It is worth noting that generally the code executes from top to bottom, unless its told to go somewhere specifically by a line, or it is returning to the start of a loop etc.
In many programs, after the imports and any initial setup, the code itself "starts" from the section that says
if __name__ == "__main__"
You can find an explanation of why here: What does if __name__ == "__main__": do?

Where to find python .difference() source code? [duplicate]

This question already has answers here:
How to read Python source code directly from IDE
(3 answers)
Finding the source code for built-in Python functions?
(8 answers)
Closed 1 year ago.
I am using macOS, conda python 3.7 with PyCharm CE IDE.
When clicking into the function, the function didn't show any source code.
Therefore, where can I find the .difference() code?
It's written in C to improve performance (pycharm doesn't have access to the cpython source code, so it can't jump to the definition), you find it here: https://github.com/python/cpython/blob/main/Objects/setobject.c#L1481
The main details of the algorithm are from line 1531 and reasonably easy to follow. It basically iterates the first set, checking if each item is in the other set, if it is, add it to a result set, then returns the result set.
The code you're looking for starts here.
First thing it does is check that the two parameters are the same length. Then it goes through the first and checks for elements that are not present in the second, building up the result as it goes. Finally, it returns said result.

Is it possible to call a function inside another script? (Python) [duplicate]

This question already has answers here:
how can I disable multiple instances of a python script?
(7 answers)
Closed 2 years ago.
When I open my program I want it to detect if the program is running already (if I launch it again while it's running). If it is I want to access the already running program and call a function there. But the problem is that I don't know how to access the already running program.
Thanks in advance!
I am confused by what you mean by "the script is the same script", but you can use import to get a function from a different file
otherprogram.py
def func(a):
return a
main.py
from otherprogram import func
print(func(5))

How to solve the error on this code?SyntaxError [duplicate]

This question already has answers here:
SyntaxError: multiple statements found while compiling a single statement
(6 answers)
Closed 5 years ago.
my English is not very good,so if I couldn't explain the question clearly,please forgive me.
I downloaded the Geany directly and used it without any setting(because there are always something wrong and I can't solve the problem.
I input code in geany,then copy and paste the in the python.
then……
Lines in the interpreter run one at a time.
Write a command, press Enter to execute it, and then write the next.
It seems that you are posting the two instructions at once instead the firs instruction (the assignment) and the the second one (the print) but I'm not sure how you can do this. I'd suggest putting them copying and pasting first one, then the other.

How to delete multiple lines in a python console window? [duplicate]

This question already has answers here:
Rewrite multiple lines in the console
(7 answers)
Closed 9 years ago.
I am currently working on a text-adventure, which should have a more or less complex fight system. To create such, I decided to generate a 16x16 tile battlefield, which will be represented by ASCII-characters. But, because a fight may take more than turn, I don't want to reprint the battlefield multiple times, but delete the "old" one and print the new situation to the same place. But I suppose that it won't work with sys.stdout.write() and sys.stdout.flush() since there have to be removed multiple lines. So my question is: how do I accomplish my goal? At the moment I open a new console window, where everything is reprinted, which is ahem... not very elegant.
Another problem would be the cross-platform use of the programm.
Check out the curses module (http://docs.python.org/2/library/curses.html).

Categories