Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 795925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:33:30+00:00 2026-05-14T22:33:30+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:33:30+00:00Added an answer on May 14, 2026 at 10:33 pm

    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 something
        elif 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 something
            elif choice == "q":
                return
    
    if __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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the following program force quit and crashes, I don't understand why, import android.app.Activity; import
How to handle the menu bar events, that is if i press quit or
I have a program that takes a list of names from a file and
all,, I have a project that is built in VB.Net 2010 and WPF 4.
I currently have a program which has the following basic structure main function --
I have a small, simple program with menu and submenus. User choose from 1-9
I have a program that runs in Python, without the console present (which is
Following is a Python script to display a menu(file,option) where option is used to
In Python 3.2, I'm writing up a basic menu program, and when the option
In my application I have a quit button, on clicking of which the session

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.