Okay everyone i am new to this post but i have a fairly easy problem (i think)... so could someone help me out? :-)

anyways heres my code in python 2.4.......

#! usr/bin/env python

import pygame, sys, os
from pygame.locals import *

pygame.init()

something = 1

def dosomething():
    something = 2

print something
dosomething()
print something

Problem with this is after i run the [dosomething] sub program it doesn't change the variable [something]

Okay as you can see i want [something] to become 2 after i run the sub but its not working!

Anyway that i can make the variable [something] able to be used in all of the program - including sub programs?

Anyways thats it but it would mean a lot if someone could help!!! Thanks!!!

Recommended Answers

All 4 Replies

You have to let your function return something and assign it after the call:

something = 1

def dosomething():
    something = 2
    return something

print something
something = dosomething()
print something

Is there a way to just like declare the variable public or something like that? Thanks for responding!!!!!!!!!

found in another forum:

>>> x = "Blah"
>>> def pp():
...   global x
...   x = "Hello There!"
...
>>> print x
Blah
>>> pp()
>>> print x
Hello There!

Declaring your variable global has certain dangers, particularly when the variable name is simple. Not good coding practice!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.