Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the difference.
#1
Having a problem I can't understand. I can print one global string but not another.

#Playfair ENDE

LETTERS = 'ABCDEFGHIKLMNOPQRSTUVWXYZ'
MESSAGE = '''Oh, I see you ate one too!'''

def main():
    print (LETTERS)
    print (MESSAGE)
    MESSAGE = '''Oh no I didn't'''
    print (MESSAGE)




if __name__ == '__main__':
    main()
When I run it I get :
Error:
ABCDEFGHIKLMNOPQRSTUVWXYZ Traceback (most recent call last): File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\Stub.py", line 16, in <module> main() File "C:\Users\jarrod0987\AppData\Local\Programs\Python\Python36-32\Stub.py", line 8, in main print (MESSAGE) UnboundLocalError: local variable 'MESSAGE' referenced before assignment
Why does LETTERS print but MESSAGE not print?
Reply
#2
Because you try to assign value to MESSAGE, thus making it different variable with local scope (local to function). Of course you can use global keyword to make it global variable, but that is considered bad style (using globals)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
So you can change it, but if you do, you change the scope?
Reply
#4
(Apr-11-2018, 05:32 AM)jarrod0987 Wrote: So you can change it, but if you do, you change the scope?

Well, that's not correct statement.

1. you can use global keyword. In this case you can change its value, you don't change the scope.

message="Message outside function"
print('value before function: {}'.format(message))
def foo():
    global message # THIS IS BAD STYLE
    message = "Message in function"
    print('value from function: {}'.format(message))
foo()
print('value after function: {}'.format(message))
Output:
value before function: Message outside function value from function: Message in function value after function: Message in function
2. as in your case, not using global, by assigning a value within the function, you effectively create different variable, with different (local) scope. So you don't change the value of the other variable named MESSAGE (one with the global scope).

message="Message outside function"
print('value before function: {}'.format(message))
def foo():
    message = "Message in function"
    print('value from function: {}'.format(message))
foo()
print('value after function: {}'.format(message))
Output:
value before function: Message outside function value from function: Message in function value after function: Message outside function
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020