python生日列表,python – 生日Paradox列表是非类型的

我正试图用

Python解决生日悖论.我很接近,但最后一块让我感到茫然.我正在使用随机生成给定范围和要创建的项目数的数字列表.这样可行.

然后我检查列表(上面生成的)是否有重复.这样可行.

然后我尝试生成给定(n)列表.这是我遇到麻烦的地方.它生成一个列表然后返回“NoneType”不可迭代.令我困惑的是,列表已生成,但Python并未将其视为列表.

这是代码:

def make_bd(n, r):

"""Generates (r) numbers of birthdays in a range (n)."""

import random

t = [random.randrange(n) for i in range(r)]

print (t)

def has_dupe(test):

"""Here I test to see if I can detect a duplicate birthday.

This is based on problem #4."""

d = []

count = 0

for number in test:

if number in d:

count = count + 1

d.append(number)

if count >= 1:

return True

return False

def dupebd(n,r,t):

count_dupe = 0

for i in range(n):

if has_dupe(make_bd(r,t)):

count_dupe = count_dupe + 1

print (float(count)/n)

dupebd(50,365,23)

这是结果:

>>> has_dupe(make_bd(50,6))

[13, 3, 8, 29, 34, 44]

Traceback (most recent call last):

File "", line 1, in

has_dupe(make_bd(50,6))

File "", line 7, in has_dupe

for number in test:

TypeError: 'NoneType' object is not iterable

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值