question

inhahe inhahe at gmail.com
Fri May 30 03:09:43 EDT 2008


"Gandalf" <goldnery at gmail.com> wrote in message 
news:13801bd3-50da-4773-a0aa-edcaf27e0c4d at 34g2000hsf.googlegroups.com...
> On May 30, 12:14 am, John Henderson <jhenRemoveT... at talk21.com> wrote:
>> Gandalf wrote:
>> > how do i write this code in order for python to understand it
>> > and print me the x variable
>>
>> > x=1
>> > def aaaa():
>> >     x++
>> >     if x > 1:
>> >         print "wrong"
>> >     else :
>> >         print x
>>
>> > aaaa()
>>
>> Example:
>>
>> x=1
>> def aaaa(x):
>>     x += 1
>>     if x > 1:
>>         return "wrong"
>>     else :
>>        return x
>>
>> print aaaa(x)
>>
>> John
>
> mmm isn't their any global variable for functions?

how about
def aaaa():
  y = x+1
  if y > 1:
    return "wrong"
  else:
    return y

but if you really have to modify x outside of the scope of the function, 
well, i guess one possibility is to return a tuple, one element being a 
string that may or may not contain the word "wrong" and another being x, and 
then whatever's calling aaaa can change x in their own scope based on the 
return value. but other than that, the normal way to affect a variable 
beyond the scope of your function is to make a class and affect a class 
variable.  although there is always the 'global' statement also.






More information about the Python-list mailing list