[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> </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> print =
'Shiplist'<BR> print '1. =
Interceptor'<BR> =20
print '2. Torpedo'<BR> print '3.=20
Warfrigate'<BR> a =3D int(raw_input ("Choose your ship =
"))<BR> if a =3D=3D=20
1:<BR> =20
Interceptor()<BR> elif a =3D=3D=20
2:<BR> =
Torpedo()<BR> =20
elif a =3D=3D 3:<BR> =20
Warfrigate()<BR> =20
else:<BR> print 'under=20
construction'</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>def Shipcost():<BR> =
print=20
'cost:', g, 'metal'<BR> print 'cost:', h,=20
'crystal'</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>def =
Interceptor():<BR> print=20
'Interceptors'<BR> b =3D int(raw_input ("How many?=20
"))<BR> g =3D 1000*b<BR> h =3D=20
0<BR> Shipcost() </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </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> </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> </DIV>
<DIV><FONT face=3DArial size=3D2>Hope someone can help</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>Thomas U.=20
Torp</FONT></DIV></FONT></DIV></BODY></HTML>
------=_NextPart_000_0038_01C0AD9B.149D6790--