import random
def findSameBDay():
birthdays = []
birthday = random.randint(1, 365)
count = 1
while birthday not in birthdays:
count += 1
birthdays.append(birthday)
birthday = random.randint(1, 365)
return count
我想把while循环改成for循环,我试着把它转换成for循环,但是我没有任何线索
Tags:
您可以使用迭代器
但是很恶心
您可以使用以下方法:
for
循环中每次调用它时都获得一个新的随机生日findSameBDay()
。对于get_birthdays
(生成器)生成的每个数字,您将把它保存在birthdays
函数参数中。当局部变量birthday
位于生成器填充的列表中时,将出现停止条件李>相关问题 更多 >
编程相关推荐