0

Why does this program work even if I do not define the userInput variable in the global scope?

intInput = True
while intInput == True:
    try:
        userInput = int(input())
        intInput = False
    except ValueError: 
        print('You must enter an integer.')

print(userInput)  # shouldn't this fail, since userInput was defined in a block?
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    A good discussion about this possible dupe question is [here](https://stackoverflow.com/q/3611760). – Jedi Feb 01 '20 at 23:29
  • 2
    `userInput` is being defined in the global scope here; Python does not have block scope. – kaya3 Feb 01 '20 at 23:36
  • 1
    Note, for the future, that a question's title should *describe the question itself*; it shouldn't be about you, or about the general context of the question, but actually be a sentence that encapsulates the question in a nutshell, so people with the same underlying problem can find it and learn from its answers. – Charles Duffy Feb 02 '20 at 00:16
  • (OdedBD was kind enough to suggest an edit with a better title, and I'm happy to have been able to approve it). – Charles Duffy Feb 02 '20 at 00:18

3 Answers3

1

Your code works because python don't have block scopes. You can learn more about it on this thread: Block scope in Python

If you define your variable inside a while statement, it's the same thing as defining your variable at the global scope (in your case).

intInput = True
while intInput == True:
    try:
        # userInput here is global, because it's inside a while statement only
        userInput = int(input())
        intInput = False
    except ValueError: 
        print('You must enter an integer.')

print(userInput)

Basically, the variable declared inside a "block scope", will have its scope from where it is being declared.

For example, if your code was inside a function, then the userInput variable would have the function's scope, and then, your code would generate an error:

intInput = True

def do_your_thing():
    while intInput == True:
        try:
            # userInput being declared here will have the function's scope
            userInput = int(input())
            intInput = False

        except ValueError: 
            print('You must enter an integer.')

# you are trying to access userInput outside its scope
print(userInput)

Now, if you try to run this code, you'll see the error:

Traceback (most recent call last):
  File "teste.py", line 28, in <module>
    print(userInput)
NameError: name 'userInput' is not defined
Rafael Marques
  • 1,501
  • 15
  • 23
0

In Python you don't need to define the variables in the global scope. Everywhere you put var_name = value you have a new variable.

If you want to declare a global variable and then use it in function or class scope you should use the global keyword like this:

test = 123
def func()
   global a
   print(a)  # 123
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Oded BD
  • 2,788
  • 27
  • 30
0

The code you have is ok and you did define it globally since you put in in a loop instead of a function. If you defined it in a function then it would give you an error.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578