Using variables from functions outside of the function

I don't know how to use a variable which i've changed inside a function. e.g.
______________________________
x=1
def something(a):
a=a+3
return a

something(x)
print x
________________________________

This will print "1" i don't know how to make this print "4" without using the 'global' command. Is there any other way? I don't like the 'global' command as it makes things messy.
Help would be great. Thanks. -imbo-


Comments

  • : I don't know how to use a variable which i've changed inside a function. e.g.
    : ______________________________
    : x=1
    : def something(a):
    : a=a+3
    : return a
    :
    : something(x)
    : print x
    : ________________________________
    :
    : This will print "1" i don't know how to make this print "4" without using the 'global' command. Is there any other way? I don't like the 'global' command as it makes things messy.
    : Help would be great. Thanks. -imbo-

    Well the obvious answer is to try either of the following:

    x = something(x)
    print x

    or

    print something(x)


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • : : I don't know how to use a variable which i've changed inside a function. e.g.
    : : ______________________________
    : : x=1
    : : def something(a):
    : : a=a+3
    : : return a
    : :
    : : something(x)
    : : print x
    : : ________________________________
    : :
    : : This will print "1" i don't know how to make this print "4" without using the 'global' command. Is there any other way? I don't like the 'global' command as it makes things messy.
    : : Help would be great. Thanks. -imbo-
    :
    : Well the obvious answer is to try either of the following:
    :
    : x = something(x)
    : print x
    :
    : or
    :
    : print something(x)

    Cheers, i knew it had to be something simple that i couldn't see. Thanks for the help.-imbo-
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    : [code]
    : $ select * from users where clue > 0
    : no rows returned
    : [/code]
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion