Home > Mobile >  How to record current value and update it each step
How to record current value and update it each step

Time:04-29

So I have a list here

Var = ['Level 3', 'Decrease', 'Increase', 'Same']

There are four levels: from 1 to 4. What I'm trying to do is, first it starts from level 3, then following the instructions in the list decrease down to level 2 and sets level 2 as the current level, then increase to level 3 again and set it as current level and follows the next instruction and so on.

I'm stuck on how to record the current level and update it at each step.

Here's what I have so far

if Var == 'Increase':
    if Starting_level == 'Level 1':
        # return level 2
    elif Starting_level == 'Level 2':
        # return level 3
    elif Starting_level == 'Level 3':
        # return level 4
    else:
        # return level 4

if Var == 'Decrease':
    if Starting_level == 'Level 1':
        # return level 1
    elif Starting_level == 'Level 2':
        # return level 1
    elif Starting_level == 'Level 3':
        # return level 2
    else:
        # return level 3

if Level == 'Same':
    if Starting_level == 'Level 1':
        # return level 1
    elif Starting_level == 'Level 2':
        # return level 2
    elif Starting_level == 'Level 3':
        # return level 3
    else:
        # return level 4

The above code only returns the first step, but because I haven't figured out how to update the current level after changes, it just stays there.

I feel like the solution is really simple, but just can't figure it out.

CodePudding user response:

As per the question they are only 4 levels(Level 1,Level 2,Level 3,Level 4), with operations of
Increase : incrementing the level
Decrease : decrementing the level
Same : no change

Var = ['Level 3', 'Decrease', 'Increase', 'Same']

def function(current_level, operation): #function for updating the current_level
    if(operation=='Same'): #need not to change
        return current_level
    k = int(current_level[-1]) #for knowing the current level number
    if(operation=='Increase'):
        k =1
        if(k>4): #out of the range as per the question
            return -1
        return 'Level ' str(k)
    else:
        k-=1
        if(k<1):
            return -1 #out of the range as per the question
    return 'Level ' str(k-1)

current_level = Var[0]
for i in range(1,len(Var)):
    current_level = function(current_level,Var[i])
    if(current_level==-1): #as the level 4 or level 1 crossed no need of executing other operations
        break
print(current_level)

The current_level prints the last level after all operations, I returned -1 if it goes out of range(out of 4 levels), it could be easier to know.

I hope it might help you. Let me know if they are any mistakes.

CodePudding user response:

Simply store your current level in Starting_level with each instruction. Also you don't require a if for Var == 'Same'. Try this:

def my_function(l1):
    Starting_level = l1[0]
    for Var in l1[1:]:

        if Var == 'Increase':
            if Starting_level == 'Level 1':
                Starting_level = 'Level 2'
            elif Starting_level == 'Level 2':
                Starting_level = 'Level 3'
            else:
                Starting_level = 'Level 4'
    
        if Var == 'Decrease':
            if Starting_level == 'Level 3':
                Starting_level = 'Level 2'
            elif Starting_level == 'Level 4':
                Starting_level = 'Level 3'
            else:
                Starting_level = 'Level 1'
        print(Starting_level)
    
Var = ['Level 3', 'Decrease', 'Increase', 'Same']
my_function(Var)

#OUTPUT
#Level 2
#Level 3
#Level 3
  • Related