鱼C论坛

 找回密码
 立即注册
查看: 1717|回复: 2

[已解决]关于第23章动手1.

[复制链接]
发表于 2017-6-28 00:09:54 | 显示全部楼层 |阅读模式

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

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

x
要求使用递归把数字变成一个列表

  1. result = []
  2. def get_digits(n):

  3.     if n:
  4.         result.append(str(n)[-1])
  5.         print(result)
  6.         get_digits(n//10)
  7.     else:
  8.         return result.reverse()
  9.    
  10. print (get_digits(123456))
复制代码


输出是:
  1. ['6']
  2. ['6', '5']
  3. ['6', '5', '4']
  4. ['6', '5', '4', '3']
  5. ['6', '5', '4', '3', '2']
  6. ['6', '5', '4', '3', '2', '1']
  7. None
复制代码


这个None是咋回事儿呢? 郁闷了
最佳答案
2017-6-29 17:38:53
本帖最后由 ButcherRabbit 于 2017-6-29 17:41 编辑
  1. result = []
  2. def get_digits(n):

  3.     if n:
  4.         result.append(str(n)[-1])
  5.         #print(result)
  6.         return get_digits(n//10)
  7.     else:
  8.         return list(reversed(result))

  9. print(get_digits(123456))
复制代码

  1. result = []
  2. def get_digits(n):

  3.     if n:
  4.         result.append(str(n)[-1])
  5.         print(result)
  6.         get_digits(n//10)
  7.     else:
  8.         return result.reverse()
  9.    
  10. print (get_digits(123456))
复制代码


回档了,在回复一次

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-28 01:22:12 From FishC Mobile | 显示全部楼层
reverse方法没有返回值,用reversed函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-29 17:38:53 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ButcherRabbit 于 2017-6-29 17:41 编辑
  1. result = []
  2. def get_digits(n):

  3.     if n:
  4.         result.append(str(n)[-1])
  5.         #print(result)
  6.         return get_digits(n//10)
  7.     else:
  8.         return list(reversed(result))

  9. print(get_digits(123456))
复制代码

  1. result = []
  2. def get_digits(n):

  3.     if n:
  4.         result.append(str(n)[-1])
  5.         print(result)
  6.         get_digits(n//10)
  7.     else:
  8.         return result.reverse()
  9.    
  10. print (get_digits(123456))
复制代码


回档了,在回复一次

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-18 08:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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