[Tutor] Local variable --> global variable -- how to?

Thomas Ueland Torp ttorp@netpower.no
Thu, 15 Mar 2001 21:58:34 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_0038_01C0AD9B.149D6790
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Someone kght have noticed that I just asked for some help. Well, I seem =
to need more. Look at this code (it is for an online game I am playing, =
Planetarion (www.planetarion.com):

print 'PA Build Manager v.b1.0 Python Version'
# This is the shiplist
def printShiplist():
    print 'Shiplist'
    print '1. Interceptor'
    print '2. Torpedo'
    print '3. Warfrigate'
    a =3D int(raw_input ("Choose your ship "))
    if a =3D=3D 1:
        Interceptor()
    elif a =3D=3D 2:
        Torpedo()
    elif a =3D=3D 3:
        Warfrigate()
    else:
        print 'under construction'

def Shipcost():
    print 'cost:', g, 'metal'
    print 'cost:', h, 'crystal'

def Interceptor():
    print 'Interceptors'
    b =3D int(raw_input ("How many? "))
    g =3D 1000*b
    h =3D 0
    Shipcost()   =20
=20
When I run this I get this error:
NameError: global name 'g' is not defined

I do see the problem, of course. The g in Interceptor() is not global, =
only local (as I understand it). Problem is, how do I get the variables =
to become global.

Hope someone can help

Thomas U. Torp

------=_NextPart_000_0038_01C0AD9B.149D6790
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2920.0" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>Someone kght have noticed that I just =
asked for=20
some help. Well, I seem to need more. Look at this code (it is for an =
online=20
game I am playing, Planetarion (<A=20
href=3D"http://www.planetarion.com">www.planetarion.com</A>):</FONT></DIV=
>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>print 'PA Build Manager v.b1.0 Python =
Version'<BR>#=20
This is the shiplist<BR>def printShiplist():<BR>&nbsp;&nbsp;&nbsp; print =

'Shiplist'<BR>&nbsp;&nbsp;&nbsp; print '1. =
Interceptor'<BR>&nbsp;&nbsp;&nbsp;=20
print '2. Torpedo'<BR>&nbsp;&nbsp;&nbsp; print '3.=20
Warfrigate'<BR>&nbsp;&nbsp;&nbsp; a =3D int(raw_input ("Choose your ship =

"))<BR>&nbsp;&nbsp;&nbsp; if a =3D=3D=20
1:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Interceptor()<BR>&nbsp;&nbsp;&nbsp; elif a =3D=3D=20
2:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
Torpedo()<BR>&nbsp;&nbsp;&nbsp;=20
elif a =3D=3D 3:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Warfrigate()<BR>&nbsp;&nbsp;&nbsp;=20
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print 'under=20
construction'</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>def Shipcost():<BR>&nbsp;&nbsp;&nbsp; =
print=20
'cost:', g, 'metal'<BR>&nbsp;&nbsp;&nbsp; print 'cost:', h,=20
'crystal'</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>def =
Interceptor():<BR>&nbsp;&nbsp;&nbsp; print=20
'Interceptors'<BR>&nbsp;&nbsp;&nbsp; b =3D int(raw_input ("How many?=20
"))<BR>&nbsp;&nbsp;&nbsp; g =3D 1000*b<BR>&nbsp;&nbsp;&nbsp; h =3D=20
0<BR>&nbsp;&nbsp;&nbsp; Shipcost()&nbsp;&nbsp;&nbsp; </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>When I run this I get this =
error:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>NameError: global name 'g' is not=20
defined</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I do see the problem, of course. The g =
in=20
Interceptor() is not global, only local (as I understand it). Problem =
is, how do=20
I get the variables to become global.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Hope someone can help</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Thomas U.=20
Torp</FONT></DIV></FONT></DIV></BODY></HTML>

------=_NextPart_000_0038_01C0AD9B.149D6790--