开发者

Python - Problem with variables

sorry if this is an obvious question, but I am relatively new to python and am completely stumped at the moment. I am having difficulty getting my variables to work the way I want them too in a program I am working on.

Here is an example I quickly made to showcase my problem:

life = 200

def example(dudeHP):
    dudeHP = dudeHP - 75
    print (dudeHP)

example(life)
print (life) #output is 开发者_开发技巧200, but I want it to be 125

As you can see, I am trying to get the value of the global variable "life" to change inside the def example codeblock with dudeHP (which would be a local variable if I am not mistaken?)

Is this viable? Is there a better way to accomplish this? Or am I doing it all wrong? Thankyou in advance :)


Well, first of all, you'll probably want to write an class or something to organize all this at some point. Modifying global variables from within functions or methods is usually considered a bad idea.

What you might want to try is this:

life = 200

def example(x):
    return x - 75

life = example(life)

Of course in this case the operation of subtraction is so trivial that you don't need to encapsulate it in a function.


BTW, the question is not Python3 specific unless a newcomer gets stumped by the title. It is a general Python language question.

You have to understand the scope and the nature of the variables in Python. Variables are just names to hold certain values and their life is within the scope of the block they are defined it, although the inner scope variable can access the variable from the outer scope. The function you have defined has its own scope and it's own stack to maintain and you are passing a copy of the variable to that function.

The best way to deal with this is return value from the function as pointed out in another answer.

Having said that, if you really want to access variable in a global manner, you could do like this.

a = 200

def example(b):
    global a
    a = a - 25

example(a)
print a

But this is frowned upon and not a good style. There is also a trick which you are use, but that goes beyond the beginner level and it goes like this. a = [200]

def example(a):
    a[0] = a[0] - 25

example(a)
print a[0]

You can easily make mistakes you if adopt this way,

But the correct way to go about this is still:

a = 200

def example(b):
    return b - 25

print example(a)


Further to Ned Batchelders and Kristoff's hints and suggestions to use a class, this is a way you could do that:

class Dude:
    def __init__(self, hp=200):
        self.hp = hp

    def eat(self):
        self.hp = self.hp + 25

def example(dude):
    dude.hp -= 75

hero = Dude()
example(hero)
hero.eat()
print(hero.hp)   # 150

Unless you have a good reason why example should be a stand-alone function, you should make it a method of Dude like eat, since example clearly acts on and in a sense belongs to an object of the class Dude.


In your function, the name dudeHP is local to the function. When you change it, you are only changing the local name, you cannot affect the value of the name passed in.

You can either make the integer be an element of a container that you pass in, so that you can change the container, or you can return the value from the function.


Since dudehp is having local scope in that function it doesn't have life outside that function. So, using dudehp to update life is illegal you can do this by using a global keyword or returing the value as others have mentioned... hope this explanation is useful and any corrections to this will be accepted.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜