Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
global var in def
#1
I have text form e and button wich save value of this form to global var

button_add = Button(root, text="+", command=button_add)
Can you explain, why this code work

def button_add():
    first_number = e.get()
    global f_num
    f_num = int(first_number)
But this not

def button_add():
    global f_num = int(e.get())
Error:

Error:
global f_num = int(e.get()) ^ SyntaxError: invalid syntax
For me they are same, but not(
Thanks!
Reply
#2
Look at the definition: 7.12. The global statement. The definition allows to declare a variable as global, but not to assign a value to it. So you must do that in two lines.
Reply
#3
https://docs.python.org/3/reference/simp...-statement
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Global variable does not seem to be global. Columbo 6 3,671 Jul-15-2019, 11:00 PM
Last Post: Columbo

Forum Jump:

User Panel Messages

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