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 7602337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:23:12+00:00 2026-05-30T23:23:12+00:00

How would I make a function where one variable (exit) determines whether a ‘while’

  • 0

How would I make a function where one variable (exit) determines whether a ‘while’ loop runs or not and be able to change it in a different function so that the while loop in the first function will stop

    while exit == 0:
       option = (raw_input("What would you like to do? "))
       if option == exit:
          exit()
    def exit():
       exit = 1

That is just an example of what I am trying to do.

When I try running it it doesnt leave the while loop and in doing so doesn’t end the program. How can I make it so that the while loop recognizes that exit is now 1 and leaves the loop?
Sorry if the question isn’t very good because this is my first time using stackoverflow to ask a question.

ADDITION: I want to kno how to change the variable across functions because I will be doing that in parts of the program. Also, I am going to be saving data before exit so that is why I have the ‘exit’ in a different function

  • 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-30T23:23:13+00:00Added an answer on May 30, 2026 at 11:23 pm

    Your problem here is scope – exit is local to the function exit() so doesn’t affect the variable exit in the loop scope.

    A better solution is this:

    exit = False
    while not exit:
        option = raw_input("What would you like to do?")
        if option == "exit":
            exit = True
    

    Or simply:

    while True:
        option = raw_input("What would you like to do?")
        if option == "exit":
            break
    

    Note the use of True and False over 1 and 0 – this is more pythonic, as what you mean here is truth values, not integers. I also changed to compare to the string "exit", as I presumed that was what you wanted, not comparing the user input to the value of exit.

    If your problem is you want to have the same scope, you might want to make your code part of a class.

    class Doer: #Naturally, give it a real name based on it's purpose.
        def __init__(self):
            self.exit = False
            while not self.exit:
                option = raw_input("What would you like to do?")
                if option == "exit":
                    self.exit()
    
        def exit(self):
            self.exit = True
    

    Here exit is an instance variable (hence being accessed from self) and therefore both times we are referencing the same variable.

    If your code is not a simplification of a more complex problem, this is largely overkill, as one of the first solutions would be more appropriate.

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

Sidebar

Related Questions

I edited the question so it would make more sense. I have a function
I would like to make a ListBox function like a Grid . Each time
What would i need to put in the SortIP function to make the custom
$(#tab1).click(function(){ loadTab(1); $('div.HOMEtabdiv ul.HOMEtabs a').removeClass('selected'); $(this).addClass('selected'); }); Would it be possible to make the
I would make to make a small WYSIWYG editor similar to the one used
While I understand the $this variable is not available when a method is called
In a GNU makefile, I would like to set an output variable to one
i would like make a simple script that store one dynamic image (Random) in
So I would like for dashboard to be able to be modified inside one
I'm having some weird issues with pytz's .localize() function. Sometimes it wouldn't make adjustments

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.