I have this code that adds 10 to either the variable x or y but it also has a method that prevents those values to go above 100 or below 0. The program asks for user input about which value to increment either x or y values:
class X_increment(object):
x=0
y=0
def x_y(self,min=0,max=100):
if self.x<=min:
self.x=0
elif self.x>=max:
self.x=100
if self.y<=min:
self.y=0
elif self.y>=max:
self.y=100
x1=X_increment()
x2=X_increment()
while (True):
z=input("Press x to increase x")
if z=='x':
x1.x+=10
x1.y-=5
x1.x_y()
print(x1.x,x1.y)
elif z=='y':
x1.y+=10
x1.x-=5
x1.x_y()
print(x1.x,x1.y)
elif z=='e':
break
else:
print("not valid")
print("the value of x and y is:",x1.x,x1.y)
print("the value of x and y of the x2 instance is",x2.x,x2.y)
I was using it to test how to modify values of an instance and it worked, but i tried the same code but initializing the variables like this:
def __init__(self):
x=0
y=0
And it didn't work, i tried it calling the variables in multiple ways but it just didn't work so i figured there is something i was doing wrong or maybe you can't change variables in a "constructor" like i am tryingto do?, i don't know, i am pretty new to programming and python and i really get confused by this.