>>> def Dec2Bin(dec):
temp=[]
if dec//2==0:
return temp.append(1)
else:
quo=dec%2
temp.append(quo)
return Dec2Bin(dec//2)
return temp
>>> Dec2Bin(9)
>>> def Dec2Bin(dec):
temp=[]
if dec//2==0:
return temp.append(1)
else:
quo=dec%2
temp.append(quo)
return Dec2Bin(dec//2)
print(temp)
>>> Dec2Bin(9)
>>> temp=[]
>>> def Dec2Bin2(dec):
global temp
if dec//2==0:
return temp.append(1)
else:
quo=dec%2
temp.append(quo)
return Dec2Bin(dec//2)
return temp
>>> Dec2Bin2(9)
>>> temp
[1]
>>> temp
[1]
>>> temp=[]
>>> def Dec2Bin_2(dec):
global temp
if dec//2==0:
return temp.append(1)
else:
quo=dec%2
temp.append(quo)
return Dec2Bin_2(dec//2)
return temp
>>> Dec2Bin_2(9)
>>> temp
[1, 0, 0, 1]
>>> def Dec2Bin(dec):
temp=[]
result=''
if dec//2==0:
return temp.append(1)
else:
quo=dec%2
temp.append(quo)
return Dec2Bin(dec//2)
while temp:
result=result+str(temp.pop())
return result
>>> Dec2Bin(9)
>>>
这个执行了还是没有返回额,temp.append(quo)这执行的时候,是往下走的时候,还是返回的时候。 |