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 inside the health?
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 :
class Health():
def __init__(self, value):
self.hp = value
def __repr__(self):
return 'You have {}.'.format(self.hp)
health = Health(100)
hp_clone = health
health.hp -= 50
print 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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments