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.
Related
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?
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.
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?
This question already has answers here:
How to disable Python warnings?
(12 answers)
Closed 8 years ago.
I have some python code that, at some point, takes the axis means of a reasonably sparse array with nans in. Every time I run the code it raises a RuntimeWarning.
I know what's causing the warning, it's intentional, and it doesn't affect the output. It is, however, quite irritating to be presented with the warning every time I run the program - so, is there a cheap and nasty way to prevent them from being printed to the terminal?
This may be useful to you, I think the issue has been pretty solved in this question: How to disable python warnings
All what you need is suppress it exactly as described in official documentation: https://docs.python.org/2/library/warnings.html#temporarily-suppressing-warnings
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).