鱼C论坛

 找回密码
 立即注册
查看: 2953|回复: 7

[已解决]我为什么又错了???!!!十进制转换二进制练习

[复制链接]
发表于 2020-2-28 19:24:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. def lulu(x):
  2.     list1 = list[]
  3.     while x//2 != 0:
  4.         a = x % 2
  5.         list1.append(a)
  6.         x = x//2
  7.         
  8.     list1.reverse()
  9.     list1.insert(0,1)
  10.     length = len(list1)
  11.     result = ''
  12.     b=0
  13.     while length:
  14.         result += str(list1[b])
  15.         length -= 1
  16.         
  17.     print(result)
复制代码
最佳答案
2020-2-28 19:40:15
Siren0327 发表于 2020-2-28 19:39
可是改了这个下面的还是不对,输入2 出来11,输入5出来111

用这段代码:

  1. def lulu(x):
  2.     list1 = []
  3.     while x:
  4.         a = x % 2
  5.         list1.append(a)
  6.         x = x // 2
  7.         print(list1)
  8.     length = len(list1)
  9.     result = ''
  10.     while length:
  11.         result += str(list1[length - 1])
  12.         length -= 1

  13.     print(result)


  14. lulu(62)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-28 19:27:11 | 显示全部楼层
  1. list1 = list[]
复制代码


这一句有误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-28 19:28:32 | 显示全部楼层
list是一个内置函数,应该这样写:
  1. def lulu(x):
  2.     list1 = [] # 将list1赋值为一个列表
  3.     while x//2 != 0:
  4.         a = x % 2
  5.         list1.append(a)
  6.         x = x//2
  7.         
  8.     list1.reverse()
  9.     list1.insert(0,1)
  10.     length = len(list1)
  11.     result = ''
  12.     b=0
  13.     while length:
  14.         result += str(list1[b])
  15.         length -= 1
  16.         
  17.     print(result)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-28 19:30:08 | 显示全部楼层
建议楼主找错时候,把你的错误代码一起贴上来,有助于帮助你快速找错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-28 19:32:53 | 显示全部楼层
  1. def lulu(x):
  2.     list1 = []
  3.     while x:
  4.         a = x % 2
  5.         list1.append(a)
  6.         x = x // 2
  7.         print(list1)
  8.     length = len(list1)
  9.     result = ''
  10.     while length:
  11.         result += str(list1[length - 1])
  12.         length -= 1

  13.     print(result)


  14. lulu(62)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-28 19:38:22 | 显示全部楼层
两个问题
你代码中列表初始化有语法错误
后面循环中b没有自增1
  1. def lulu(x):
  2.     list1 = list()
  3.     while x//2 != 0:
  4.         a = x % 2
  5.         list1.append(a)
  6.         x = x // 2
  7.         
  8.     list1.reverse()
  9.     list1.insert(0,1)
  10.     length = len(list1)
  11.     result = ''
  12.     b=0
  13.     while length:
  14.         result += str(list1[b])
  15.         b = b +1
  16.         length -= 1
  17.         
  18.     print(result)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-28 19:39:55 | 显示全部楼层

可是改了这个下面的还是不对,输入2 出来11,输入5出来111
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-28 19:40:15 | 显示全部楼层    本楼为最佳答案   
Siren0327 发表于 2020-2-28 19:39
可是改了这个下面的还是不对,输入2 出来11,输入5出来111

用这段代码:

  1. def lulu(x):
  2.     list1 = []
  3.     while x:
  4.         a = x % 2
  5.         list1.append(a)
  6.         x = x // 2
  7.         print(list1)
  8.     length = len(list1)
  9.     result = ''
  10.     while length:
  11.         result += str(list1[length - 1])
  12.         length -= 1

  13.     print(result)


  14. lulu(62)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-1 19:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表