[Tutor] Local variable --> global variable -- how to?
Deirdre Saoirse
deirdre@deirdre.net
Thu, 15 Mar 2001 13:35:15 -0800 (PST)
On Thu, 15 Mar 2001, Thomas Ueland Torp wrote:
> def Shipcost():
> print 'cost:', g, 'metal'
> print 'cost:', h, 'crystal'
>
> def Interceptor():
> print 'Interceptors'
> b = int(raw_input ("How many? "))
> g = 1000*b
> h = 0
> Shipcost()
>
> 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.
1) bad idea to make variables global unless there is no other way.
2) there is nearly always another way.
3) Pass g and h to the Shipcost function:
def Shipcost(gold, crystal):
print 'cost:', gold, 'metal'
print 'cost:', crystal, 'crystal'
def Interceptor():
print 'Interceptors'
b = int(raw_input ("How many? "))
g = 1000*b
h = 0
Shipcost(g, h)
--
_Deirdre NEW Stash-o-Matic: http://fuzzyorange.com http://deirdre.net
"I love deadlines. I like the whooshing sound they make as they fly by."
- Douglas Adams