0

I am new to python. This is a small example of what I'm wanting to do in my code. I want to save the values of square and circle. So it will ask "do want to change the values..." you press one for square, and it goes from 0 to 1, then it ask again, you press two, then it goes up again to 2. I do not want globals in my program. I am reading post that say the answer to no-globals is to pass varibles to functions and make changes then return them. I dont think that would work with my while loop.

loopcounter = 0
square = 0
circle = 0

 def varibleChanges(circle, square):
    #global circle, square
    print 'Would you like to change circle or square?'
    print '1. circle' '\n' '2. square'
    choice = raw_input('>>')
    if choice == '1':
        square = square + 1
    elif choice == '2':
    circle = circle + 1
    print 'square: ', square 
    print 'circle: ', circle

while loopcounter <=2:
    print 'this is the begining of the loop'
    varibleChanges(circle, square)
    loopcounter +=1
    print "this is the end of the loop\n"

Would storing the varibles outside the code work, like writing to file, (I will have a save feature anyways) Or would it be best to rethink the code again?

3 Answers 3

3

Though it's not necessary for a program of your size, you might consider using classes.

class Circle(object):
    def __init__(self, value):
        self.value = value
    def getValue(self):
        return self.value
    def incValue(self, add):
        self.value += add

circle = Circle(0) #Create circle object
circle.incValue(1)
print(circle.getValue())

Classes will be much more useful when you deal with larger programs. If you have multiple circles, for example, you would create many circle objects from this circle class. You can then deal with each circle separately.

You're probably better off with one of the simpler answers for now, but you will definitely use classes eventually.

See here to learn about classes in Python.

Sign up to request clarification or add additional context in comments.

Comments

2

Returning the variables, then passing them back in, will work just fine with your code. If you modify your code to be the following:

def varibleChanges(circle, square):
    #skip to the end..
    return circle, square

while loopcounter <=2:
    print 'this is the begining of the loop'
    circle, square = varibleChanges(circle, square)
    loopcounter +=1
    print "this is the end of the loop\n"

Then you should see the behavior you want.

As a side note, you can write things like:

circle = circle + 1

as

circle += 1

in Python. Happy Coding!

2 Comments

Wow, it works! so I'm guessing that only varibles saved at the first level of the loop are changed, and everything else is reset.
Yes, in a manner of speaking. The concept to read up on is 'scoping'. The variables inside a function cannot be seen outside of the function (their scope is restricted) unless you pass them out of the function (or they are passed by reference). I recommend that you read Dive Into Python (diveintopython.net). It will cover topics as to how scopes work, which python types are passed by reference or value, and other important concepts.
1

If variableChanges returned a tuple of: the name of the shape it wanted to modify, and the new value, then shapes would not need to be global, or even be available in variableChanges.

 def variableChanges(circle, square):
    print 'Would you like to change circle or square?'
    print '1. circle' '\n' '2. square'
    choice = raw_input('>>')
    if choice == '1':
        return ('square', square + 1)
    elif choice == '2':
        return ('circle', circle + 1)

loopcounter = 0
shapes = {
    'square' = 0,
    'circle' = 0
}

while loopcounter <= 2:
    print 'this is the begining of the loop'
    shape, value = variableChanges(shapes['circle'], shapes['square'])
    shapes[shape] = value
    print (shape, value)
    loopcounter += 1
    print "this is the end of the loop\n"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.