global variable not assigned, newbie question

  • Thread starter Jean-Daniel Gamache
  • Start date
J

Jean-Daniel Gamache

Hi! here's a python newbie question ...
why is my variable (myNum) not assigned the value 10 ???

========

myNum = 0

def setNum():
myNum = 10
print myNum

def main():
setNum()
print myNum

if __name__ == '__main__':
main()
 
D

Diez B. Roggisch

You need myNum to be declared global:

def setNum():
global myNum
myNum = 10
print myNum

Apart from that, its usually considered bad style using global variables -
better use this:

def calcNum():
myNum = 10
return myNum

myNum = calcNum()
 
P

Peter Hansen

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top