你是我的小可爱 发表于 2020-2-21 16:17:06

求助

def Dec2Bin(dec):
    temp = []

    while dec:
      quo = dec % 2
      dec = dec // 2
      temp.append(quo)
      return temp

print(Dec2Bin(62))
这个为什么返回的值是0,而不是011111

°蓝鲤歌蓝 发表于 2020-2-21 16:19:33

因为只循环了一次,碰到return 就退出函数了。

你是我的小可爱 发表于 2020-2-21 16:21:46

那在请问一下如果我想输出我要的011111结果该怎么改,我试了好多次都不行{:5_109:}

zltzlt 发表于 2020-2-21 19:46:25

把 return 减少一个缩进就行了。

def Dec2Bin(dec):
    temp = []

    while dec:
      quo = dec % 2
      dec = dec // 2
      temp.append(quo)
    return temp


print(Dec2Bin(62))

你是我的小可爱 发表于 2020-2-22 12:33:39

万分感谢!
页: [1]
查看完整版本: 求助