免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4011 | 回复: 6

让PYTHON做决定留否 [复制链接]

论坛徽章:
1
IT运维版块每日发帖之星
日期:2016-05-06 06:20:00
发表于 2011-05-31 14:37 |显示全部楼层
本帖最后由 jordie 于 2011-05-31 14:39 编辑

最近没心思上班,一直拿不定主意,就用PYTHON写了一个命运选择器:
  1. #!/usr/bin/python
  2. import random

  3. print '################################################################################'
  4. print '''Welcome to mychoice! If you hard to make a decision,Just try this MyChoice!'''
  5. print '################################################################################'
  6. choice1 = raw_input('Input one of you choice:')
  7. choice2 = raw_input('Input the second choice:')
  8. counttimes = raw_input('Input the times you want to count:')
  9. choice1_times = 0
  10. choice2_times = 0

  11. for i in range(0,int(counttimes)):
  12.     if random.choice([choice1,choice2]) == choice1:
  13.         choice1_times += 1
  14.     else:
  15.         choice2_times += 1
  16. print '%s is %s times, and %s is %s times' %(choice1,choice1_times,choice2,choice2_times)
  17. choices={choice1:choice1_times,choice2:choice2_times}
  18. print '*******************************************'
  19. print 'You choice is',max(choices.iterkeys(),key = lambda k: choices[k])
  20. print '*******************************************'
复制代码
  1. ################################################################################
  2. Welcome to mychoice! If you hard to make a decision,Just try this MyChoice!
  3. ################################################################################
  4. Input one of you choice:staying
  5. Input the second choice:leaving
  6. Input the times you want to count:10000000
  7. staying is 4999691 times, and leaving is 5000309 times
  8. *******************************************
  9. You choice is leaving
  10. *******************************************
复制代码

论坛徽章:
0
发表于 2011-05-31 19:53 |显示全部楼层

论坛徽章:
0
发表于 2011-06-01 07:37 |显示全部楼层
抓阄选择器..........

论坛徽章:
0
发表于 2011-06-01 20:41 |显示全部楼层
分歧终端选择器

今天试了一下跑步或者不跑步
结果四次都是不跑

论坛徽章:
0
发表于 2011-06-02 09:51 |显示全部楼层
key = lambda k: choices[k]
程序里这句话是什么意思?
max()是返回迭代器中最大的值是吗 ,后面的是表示什么意思?是关于value的?

论坛徽章:
0
发表于 2011-06-21 19:02 |显示全部楼层
此程序存在BUG,算法有问题,并不能真正随机做出决定

最后的决定永远是choice2,无论choice1和choice2的内容,以及counttimes的数值是什么。

论坛徽章:
0
发表于 2011-06-29 17:19 |显示全部楼层
回复 6# Sackula


    请问下有什么bug
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP