Search code examples
pythonlistnumbers

Convert number to another number in n steps


I want to make a gradient function, and I need the R G and B values to change the same number of times. My current code (python) is this:

def a2bn(a:int, b:int, n:int):
    __l = []
    __c = 0
    for i in range(n):
        __c += (b/n)
        __l.append(__c)
    return(__l)

However, it doesn't work because it can't have a starting value (A) without messing up the list. (the function takes in single numbers, but I can use three of them to combine the lists).


Solution

  • If you want there to be a gradient from a to b, shouldn't you initialize _c=a and then update by _c += (b-a)/n? – Isdj

    This worked, thanks! (tomorrow this will be marked as answer)