0

I am using python 2. I don't know what is wrong with my code, wrong message is "local variable y referenced before assignment". But variable y is assigned before referenced and there is no problem for variable x. Could you help to figure it out?

  def dyn_seq(n):
        C=[0,0,1,1]
        for i in range(4,n+1):
            z=C[i-1]+1
            if i % 2==0: 
                x=C[i/2]+1
            if i % 3==0:
                y=C[i/3]+1
            minu=min(x,y,z)
            C.append(minu)
        return C
Liang Li
  • 3
  • 6

1 Answers1

1

You are doing a modulo division and your running variable starts at 4. 4 % 2 == 0 and x will be defined, while 4 % 3 != 0 and y is not defined, but used in the next line.

Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99