Skip to content Skip to sidebar Skip to footer

How Do I Change A Variable Inside A Variable?

Here's my code : hp1 = 100 health1 = 'you have', hp1 hp1 = hp1 - 50 health1 print hp1 print health1 This is what it prints : 50 ('you have', 100) Why doesn't the hp1 change ins

Solution 1:

To automatically change the output with any mutations of hp1, you can use a class:

classHealth:def__init__(self, health):
       self.health = health
   def__add__(self, val):
       return Health(self.health + val)
   def__sub__(self, val):
       return Health(self.health - val)
   def__repr__(self):
       return"you have {}".format(self.health)

hp1 = Health(100)
hp1 -= 50
print(hp1)

Output:

you have 50

Solution 2:

The following line:

health1 = 'you have', hp1

Is creating a tuple with two values: "you have" and 100 (Note that the value of hp1 is copied, and not referenced). It's then assigning this tuple to a new variable named health1.

health1 has nothing to do with hp1. If hp1 get overriden, deleted, thrown away, or anything happens to it, health1 doesn't care.


If you are so eager to pass this variable a reference, you can create a wrapper class around the int type:

classIntWrapper(object):
     def__init__(self, value):
          self.value = value
     def__add__(self, value):
          return IntWrapper(self.value + value)
     def__iadd__(self, value):
          self.value += value
          return self
     def__sub__(self, value):
          return IntWrapper(self.value - value)
     def__isub__(self, value):
          self.value -= value
          return self
     def__str__(self):
          returnstr(self.value)
     def__repr__(self):
          returnstr(self)

hp1 = IntWrapper(100)
health1 = 'you have', hp1

hp1 -= 50print hp1          # 50print health1      # ('you have', 50)

Solution 3:

To do what you wish to do, you must use a class. This is the closest form of a pointer you will encounter in python.

Here is an example :

classHealth():
    def__init__(self, value):
        self.hp = value

    def__repr__(self):
        return'You have {}.'.format(self.hp)

health = Health(100)
hp_clone = health
health.hp -= 50print hp_clone
# Program outputs : You have 50.

Your question is also a possible duplicate of Pointers in Python? .

What is happening here in your program has been explained by the others.

Solution 4:

Because you defined health1 - a (string, int) tuple - as hp1 was still 100 and didn't change it since then. This is not a pointer in C/C++ sense, just a copy by value.

Solution 5:

In your code you have done like this,

hp1 = 100 # setting hp1 as 100
health1 = 'you have', hp1 # making a tuple 

hp1 = hp1 - 50 # subracting 50 from hp1 -> gives 50 as result
health1 # simply calling health1print hp1 # displaying hp1print health1 # displaying health1

In this code,

You defined hp1 as 100, let it be stored in a location 1000

You made a tuple names health1 as 'you have', hp1. It will be stored in a location say 2000

You subtracted 50 from hp1 making hp1 50, this will make no change to health1 variable because it is stored in different location. But it will change the value of hp1

Hope this helps.!!

Post a Comment for "How Do I Change A Variable Inside A Variable?"