Home > Net >  Updating a variable value depended on the list when a certain condition is met
Updating a variable value depended on the list when a certain condition is met

Time:02-01

I am having a little problem with my code and I would be glad if someone can help me understand why this is happing and how to cope with it.

So, here is my code:

import math

class User:
    ranks = [-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8]
    i = 0
    rank = ranks[i]
    progress = 0

    def inc_progress(self, task_rank):
        if task_rank == self.rank:
            self.progress  = 3
            if self.progress >= 100:
                self.i  = math.floor(self.progress/100)
                self.progress = self.progress - math.floor(self.progress/100)*100
        elif task_rank == self.rank-1:
            self.progress  = 1
            if self.progress >= 100:
                self.i  = math.floor(self.progress/100)
                self.progress = self.progress - math.floor(self.progress/100)*100
        elif task_rank <= self.rank - 2:
            self.progress  = 0
        else:
            print(True)
            self.progress  = 10 * ((task_rank - self.rank)**2)
            if self.progress >= 100:
                print(True)
                self.i  = math.floor(self.progress/100)
                print(math.floor(self.progress/100))
                self.progress = self.progress - math.floor(self.progress/100)*100

Here, I was assuming that when the value of i changes, the rank will also change along with it. However it is not the case.

user = User()
user.inc_progress(-4)
print(user.progress) /*==> returns 60 which is as expected*/
print(user.rank) /*==> returns -8 which should be -7 since the value of i changes*/
print(user.i) /*==> returns 1 as expected

CodePudding user response:

The line rank = ranks[i] is executed just once, at the time of the class instance initialization. after that rank and i variables are independent of each other. You can define rank not as an attribute but as a method of the class, as below. Note that you need to call self.rank() instead of self.rank

import math

class User:
    ranks = [-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8]
    i = 0
    
    progress = 0

    def rank(self):
      return self.ranks[self.i]

    def inc_progress(self, task_rank):
        if task_rank == self.rank():
            self.progress  = 3
            if self.progress >= 100:
                self.i  = math.floor(self.progress/100)
                self.progress = self.progress - math.floor(self.progress/100)*100
        elif task_rank == self.rank()-1:
            self.progress  = 1
            if self.progress >= 100:
                self.i  = math.floor(self.progress/100)
                self.progress = self.progress - math.floor(self.progress/100)*100
        elif task_rank <= self.rank() - 2:
            self.progress  = 0
        else:
            print(True)
            self.progress  = 10 * ((task_rank - self.rank())**2)
            if self.progress >= 100:
                print(True)
                self.i  = math.floor(self.progress/100)
                print(math.floor(self.progress/100))
                self.progress = self.progress - math.floor(self.progress/100)*100

CodePudding user response:

I have updated the code, which is working fine now. Try it online!

The problem was that you are updating i but not updating rank accordingly. To make the rank also change with i, you can update the rank using the ranks list and the updated i value. Here's an updated version of the code:

import math

class User:
    ranks = [-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8]
    i = 0
    rank = ranks[i]
    progress = 0

    def inc_progress(self, task_rank):
        if task_rank == self.rank:
            self.progress  = 3
            if self.progress >= 100:
                self.i  = math.floor(self.progress/100)
                self.rank = self.ranks[self.i]
                self.progress = self.progress - math.floor(self.progress/100)*100
        elif task_rank == self.rank-1:
            self.progress  = 1
            if self.progress >= 100:
                self.i  = math.floor(self.progress/100)
                self.rank = self.ranks[self.i]
                self.progress = self.progress - math.floor(self.progress/100)*100
        elif task_rank <= self.rank - 2:
            self.progress  = 0
        else:
            self.progress  = 10 * ((task_rank - self.rank)**2)
            if self.progress >= 100:
                self.i  = math.floor(self.progress/100)
                self.rank = self.ranks[self.i]
                self.progress = self.progress - math.floor(self.progress/100)*100

user = User()
user.inc_progress(-4)
print(user.progress) #/*==> returns 60 */
print(user.rank)# /*==> returns -7 since the value of i changes*/
print(user.i) #/*==> returns 1

Here's the output: Try it online!

60 -7 1

Now the rank should also change along with i whenever the progress is increased.

  • Related