Python 嵌套循环问题
Python nested loops issues
我正在努力学习 Python 并且我正在编写一些脚本以适应这门语言。
我正在尝试创建一个将滚动 3 'dice' 的脚本,如果所有 3 return 6,它会将其写入列表,否则再次滚动并更新计数器。
所有这些都应该发生很多次,这样我就可以得到一个大列表,然后计算获得三重 6 所需的平均掷骰数。
经过多次迭代后,这就是我现在的代码(可能不是最理想的,因为我对其进行了大量编辑以尝试找到使其工作的方法)
#!/usr/bin/python
from random import randint
first = randint(0,5)
second = randint(0,5)
third = randint(0,5)
count = 1
list = []
for_list = range(10000)
for item in for_list:
while first != 5 or second != 5 or third != 5:
count+=1
first = randint(0,5)
second = randint(0,5)
third = randint(0,5)
if first == 5 and second == 5 and third == 5:
list.append(count)
count = 1
print sum(list) / float(len(list))
print list
现在似乎 while 循环有效,但我不知道如何让它实际 运行 多次(for 循环,在这个例子中是 10,000 次)。
这是我的输出(打印 "average" 和包含计数变量的列表:
218.0
[218]
所以在这个 运行 中需要 218 卷。之后脚本结束。
谁能帮我理解为什么脚本没有 运行for 循环?
谢谢!
您的布尔条件在这里有问题:
first != 5 or second != 5 or third != 5
只有当所有三个值都设置为 5
时才会 False
。当你找到一个 triple-5 集时,你的 while
循环是 always 将是 False
.
因此,在找到您的第一个匹配项后,您的 first
、second
和 third
变量将设置为 5
,并且 while
循环永远不会进入其他 9999 次你的 for
循环迭代。
与其在此处使用 while
循环,不如使用大量迭代并在每次迭代中掷骰子,然后使用简单的 if
将其添加到列表中:
results = []
counter = 0
for iteration in xrange(10 ** 6): # 1 million times
counter += 1
roll = random.randint(1, 6), random.randint(1, 6), random.randint(1, 6)
if roll == (6, 6, 6):
results.append(counter)
counter = 0
我为结果列表使用了更好的名称;您希望避免使用与变量的内置类型相同的名称。既然你用的是Python 2,我也改用xrange()
;仅仅为了控制循环应该重复多少次而建立一个完整的整数列表是没有意义的。
我用一个元组来存储三个骰子,并从 1 到 6 中挑选数字来匹配常规的骰子数字。然后,您可以通过与另一个包含 3 个数字的元组进行比较来测试是否找到匹配项。
演示:
>>> results = []
>>> counter = 0
>>> for iteration in xrange(10 ** 6): # 1 million times
... counter += 1
... roll = random.randint(1, 6), random.randint(1, 6), random.randint(1, 6)
... if roll == (6, 6, 6):
... results.append(counter)
... counter = 0
...
>>> sum(results, 0.0) / len(results)
217.40704500978472
>>> len(results)
4599
经过一些修改,我相信这可能是你想要的:
from random import randint
first = randint(0,5)
second = randint(0,5)
third = randint(0,5)
count = 1
list = []
for_list = 10001
completed = 0
while completed < for_list:
completed = completed+1
count=count+1
first = randint(0,5)
second = randint(0,5)
third = randint(0,5)
if first == 5 and second == 5 and third == 5:
list.append(count)
count = 1
print sum(list) / float(len(list))
print list
哪个返回了;
172.071428571
[214, 196, 44, 14, 43, 31, 427, 179, 427, 48, 134, 78, 261, 256, 36, 242, 244, 40, 189, 53, 140, 690, 26, 802, 39, 45, 2, 93, 30, 26, 351, 117, 455, 24, 190, 359, 83, 23, 60, 81, 38, 3, 173, 205, 175, 689, 233, 59, 26, 122, 263, 415, 211, 38, 94, 100]
编辑:正如 Martijn Pieters(下)所说,您可以删除
first = randint(0,5)
second = randint(0,5)
third = randint(0,5)
从循环外。
我正在努力学习 Python 并且我正在编写一些脚本以适应这门语言。
我正在尝试创建一个将滚动 3 'dice' 的脚本,如果所有 3 return 6,它会将其写入列表,否则再次滚动并更新计数器。
所有这些都应该发生很多次,这样我就可以得到一个大列表,然后计算获得三重 6 所需的平均掷骰数。
经过多次迭代后,这就是我现在的代码(可能不是最理想的,因为我对其进行了大量编辑以尝试找到使其工作的方法)
#!/usr/bin/python
from random import randint
first = randint(0,5)
second = randint(0,5)
third = randint(0,5)
count = 1
list = []
for_list = range(10000)
for item in for_list:
while first != 5 or second != 5 or third != 5:
count+=1
first = randint(0,5)
second = randint(0,5)
third = randint(0,5)
if first == 5 and second == 5 and third == 5:
list.append(count)
count = 1
print sum(list) / float(len(list))
print list
现在似乎 while 循环有效,但我不知道如何让它实际 运行 多次(for 循环,在这个例子中是 10,000 次)。
这是我的输出(打印 "average" 和包含计数变量的列表:
218.0
[218]
所以在这个 运行 中需要 218 卷。之后脚本结束。 谁能帮我理解为什么脚本没有 运行for 循环?
谢谢!
您的布尔条件在这里有问题:
first != 5 or second != 5 or third != 5
只有当所有三个值都设置为 5
时才会 False
。当你找到一个 triple-5 集时,你的 while
循环是 always 将是 False
.
因此,在找到您的第一个匹配项后,您的 first
、second
和 third
变量将设置为 5
,并且 while
循环永远不会进入其他 9999 次你的 for
循环迭代。
与其在此处使用 while
循环,不如使用大量迭代并在每次迭代中掷骰子,然后使用简单的 if
将其添加到列表中:
results = []
counter = 0
for iteration in xrange(10 ** 6): # 1 million times
counter += 1
roll = random.randint(1, 6), random.randint(1, 6), random.randint(1, 6)
if roll == (6, 6, 6):
results.append(counter)
counter = 0
我为结果列表使用了更好的名称;您希望避免使用与变量的内置类型相同的名称。既然你用的是Python 2,我也改用xrange()
;仅仅为了控制循环应该重复多少次而建立一个完整的整数列表是没有意义的。
我用一个元组来存储三个骰子,并从 1 到 6 中挑选数字来匹配常规的骰子数字。然后,您可以通过与另一个包含 3 个数字的元组进行比较来测试是否找到匹配项。
演示:
>>> results = []
>>> counter = 0
>>> for iteration in xrange(10 ** 6): # 1 million times
... counter += 1
... roll = random.randint(1, 6), random.randint(1, 6), random.randint(1, 6)
... if roll == (6, 6, 6):
... results.append(counter)
... counter = 0
...
>>> sum(results, 0.0) / len(results)
217.40704500978472
>>> len(results)
4599
经过一些修改,我相信这可能是你想要的:
from random import randint
first = randint(0,5)
second = randint(0,5)
third = randint(0,5)
count = 1
list = []
for_list = 10001
completed = 0
while completed < for_list:
completed = completed+1
count=count+1
first = randint(0,5)
second = randint(0,5)
third = randint(0,5)
if first == 5 and second == 5 and third == 5:
list.append(count)
count = 1
print sum(list) / float(len(list))
print list
哪个返回了;
172.071428571
[214, 196, 44, 14, 43, 31, 427, 179, 427, 48, 134, 78, 261, 256, 36, 242, 244, 40, 189, 53, 140, 690, 26, 802, 39, 45, 2, 93, 30, 26, 351, 117, 455, 24, 190, 359, 83, 23, 60, 81, 38, 3, 173, 205, 175, 689, 233, 59, 26, 122, 263, 415, 211, 38, 94, 100]
编辑:正如 Martijn Pieters(下)所说,您可以删除
first = randint(0,5)
second = randint(0,5)
third = randint(0,5)
从循环外。