鱼C论坛

 找回密码
 立即注册
查看: 957|回复: 1

关于函数的闭包使用方法

[复制链接]
发表于 2019-6-13 17:24:40 | 显示全部楼层 |阅读模式

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

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

x
自己写了一个计算生肖和星座的小程序
在39行使用闭包写法的话就没有运行结果了,这是什么原因呢?
闭包写法只能返回函数中 return 的值吗?如果使用闭包写法有 print 输出结果怎么写呢?以下是我的代码

  1. import os

  2. print('该程序用来计算生肖和星座')

  3. constellation = (u'摩羯座',u'水瓶座',u'双鱼座',u'白羊座',u'金牛座',u'双子座',
  4.                u'巨蟹座',u'狮子座',u'处女座',u'天秤座',u'天蝎座',u'射手座',)
  5. constellation_days = ((1,20),(2,19),(3,21),(4,21),(5,21),(6,22),
  6.                (7,23),(8,23),(9,23),(10,23),(11,23),(12,23),)
  7. zodiac = '猴鸡狗猪鼠牛虎兔龙蛇马羊'

  8. save_list = []

  9. def Save():
  10.     file1 = input('请输入要保存的文件名【需要带后缀名】:')
  11.     with open(file1,'w') as f:
  12.         f.writelines(save_list)

  13. def zodiac_china():
  14.     brith_y = input('请输入出生年份:')
  15.     brith_m = input('请输入出生月份:')
  16.     brith_d = input('请输入出生日:')
  17.     zodiac_y = zodiac[int(brith_y)%12]
  18.     print('%s 的生肖是 %s'%(name,zodiac_y))
  19.     def constell():
  20.         for num in range(len(constellation_days)):
  21.             if constellation_days[num] >= (int(brith_m),int(brith_d)):
  22.                 print('%s 的星座是:%s'%(name,constellation[num]))
  23.                 break
  24.             elif int(brith_m) == 12 and int(brith_d) > 23:
  25.                 print('%s 的星座是:%s'%(name,constellation[0]))
  26.                 break
  27.         print('是否保存记录【Y/N】')
  28.         if input() in ['Y','N','y','n']:
  29.             global save_list
  30.             save_list.extend(['姓名:',name,' ',brith_y,' 年 ',brith_m,' 月 ',
  31.                               brith_d,' 日 ',(constellation[num]or constellation[0])])
  32.             Save()
  33.             print('保存成功')
  34.     constell()

  35. name = input('请输入姓名:')
  36. zodiac_china()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-6-14 10:30:54 | 显示全部楼层
  1. def zodiac_china():
  2.     brith_y = input('请输入出生年份:')
  3.     brith_m = input('请输入出生月份:')
  4.     brith_d = input('请输入出生日:')
  5.     zodiac_y = zodiac[int(brith_y)%12]
  6.     print('%s 的生肖是 %s'%(name,zodiac_y))
  7.     constell(brith_m, brith_d)

  8. def constell(brith_m, brith_d):
  9.         for num in range(len(constellation_days)):
  10.             if constellation_days[num] >= (int(brith_m),int(brith_d)):
  11.                 print('%s 的星座是:%s'%(name,constellation[num]))
  12.                 break
  13.             elif int(brith_m) == 12 and int(brith_d) > 23:
  14.                 print('%s 的星座是:%s'%(name,constellation[0]))
  15.                 break
  16.         print('是否保存记录【Y/N】')
  17.         if input() in ['Y','N','y','n']:
  18.             global save_list
  19.             save_list.extend(['姓名:',name,' ',brith_y,' 年 ',brith_m,' 月 ',
  20.                               brith_d,' 日 ',(constellation[num]or constellation[0])])
  21.             Save()
  22.             print('保存成功')
复制代码


把用到的参数传入就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-16 09:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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