0
def choice(n,s=[]):
  a=s
  if n==0:
    string=''
    d=[str(a[len(a)-i-1]) for i in range(len(a))]
    string=string.join(d)
    print(string)
    return string
  if (n-1)%2==0:
    a.append(1)
    n=float(n-1)/2
    return choice(n,a)
  if (n-2)%2==0:
    a.append(2)
    n=float(n-2)/2
    return choice(n,a)
choice(10)#this print 122 ,it's right

choice(10)#but when I run it again ,this print 122122

I dont know what to do

please help me!!!

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
Kyan
  • 99
  • 9

1 Answers1

1

Using a mutable list as a default value is bad practice. Simply changing the top of your function like this will fix it.

def choice(n,s=None):
  if s is None:
      s = []
G_M
  • 3,342
  • 1
  • 9
  • 23