Is there a method that tells my program to quit?

2024/11/19 21:44:22

For the "q" (quit) option in my program menu, I have the following code:

elif choice == "q":print()

That worked all right until I put it in an infinite loop, which kept printing blank lines. Is there a method that can quit the program? Else, can you think of another solution?

Answer

One way is to do:

sys.exit(0)

You will have to import sys of course.

Another way is to break out of your infinite loop. For example, you could do this:

while True:choice = get_input()if choice == "a":# do somethingelif choice == "q":break

Yet another way is to put your main loop in a function, and use return:

def run():while True:choice = get_input()if choice == "a":# do somethingelif choice == "q":returnif __name__ == "__main__":run()

The only reason you need the run() function when using return is that (unlike some other languages) you can't directly return from the main part of your Python code (the part that's not inside a function).

https://en.xdnf.cn/q/26395.html

Related Q&A

Hiding Axis Labels

Im trying to hide the axis labels on the first subplot at 211. Id like to label the figure, not just a subplot (reference: "Isub Event Characteristics"). How can I control font properties lik…

Why does Python preemptively hang when trying to calculate a very large number?

Ive asked this question before about killing a process that uses too much memory, and Ive got most of a solution worked out.However, there is one problem: calculating massive numbers seems to be untouc…

Django, name parameter in urlpatterns

Im following a tutorial where my urlpatterns are:urlpatterns = patterns(,url(r^passwords/$, PasswordListView.as_view(), name=passwords_api_root),url(r^passwords/(?P<id>[0-9]+)$, PasswordInstance…

The most Pythonic way of checking if a value in a dictionary is defined/has zero length

Say I have a dictionary, and I want to check if a key is mapped to a nonempty value. One way of doing this would be the len function:mydict = {"key" : "value", "emptykey"…

What does the --pre option in pip signify?

I saw on this page that pip install neo4j-doc-manager --pre was used. What does the --pre flag mean?

Tracing and Returning a Path in Depth First Search

So I have a problem that I want to use depth first search to solve, returning the first path that DFS finds. Here is my (incomplete) DFS function:start = problem.getStartState()stack = Stack()visited =…

Pandas OHLC aggregation on OHLC data

I understand that OHLC re-sampling of time series data in Pandas, using one column of data, will work perfectly, for example on the following dataframe:>>df ctime openbid 1443654000 1.1170…

Python plotting libraries [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

python elasticsearch client set mappings during create index

I can set mappings of index being created in curl command like this:{ "mappings":{ "logs_june":{ "_timestamp":{ "enabled":"true"},"properties&…

Get Primary Key after Saving a ModelForm in Django

How do I get the primary key after saving a ModelForm? After the form has been validated and saved, I would like to redirect the user to the contact_details view which requires the primary key of the …