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

  • Home
  • SEARCH
  • 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 8860809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:20:38+00:00 2026-06-14T15:20:38+00:00

Ok, the below code is completely scrap, pointless code I made up; but I

  • 0

Ok, the below code is completely scrap, pointless code I made up; but I find it weird that I can access the variable “b” from the qwerty() function while b was only declared inside while… I thought I could only access it if I declared it globally somehow?

x = 14
while (x > 10):
    b = 3
    b += 3
    print(b)
    x -= 1

def qwerty():
    if b == 6:
         print("b can be accessed elsewhere?")

input("")

Running this code, “b can be accessed elsewhere?” will be printed… even though b == 6 is referencing the variable b that was declared in the separate while() function.

I guess I am still confused about the properties of global and local variables in python. Can anyone explain why the above happens?

  • 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-06-14T15:20:39+00:00Added an answer on June 14, 2026 at 3:20 pm

    while is not a function. The only constructs that create their own scopes are def, class, and generator expressions / comprehensions (depending on version).

    if True:
        while 1:
            for x in range(1):
                z = 1
            break
    
    def function():
        print(z)
    
    function() # prints 1
    

    This works almost the same way as inside functions.

    def function():
        while True:
            x = 5
            break
        def nested():
            print(x)
        nested() # prints 5
        x = 10
        nested() # prints 10
    

    I say almost because there are a few subtle differences in how you can modify outer variables depending on whether they are global or belong to an enclosing function.

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

Sidebar

Related Questions

below code is my databasehandler class i got it from a tutorial. Beside that
As you can see from the code below we, use TinyMce a lot in
I've made a class (code below) that handles the creation of a matching quiz
Below I present you some code which has completely been butchered by me. $(.gig).hover(function()
Below code not work, but it's work fine for jsf1.2. Now the framework is
In below code when I click 'Vote' a vote results screen is displayed but
Alright, I find the code below to be quite repetitive and annoying. Any other
The code below is the comments template from my functions.php in WP. It outputs
Please have a look at the code below (excerpt from a C# book): public
I'm trying to load loadingForm like below code. But it doesn't work, the loadingForm

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.