Python骰子滚动模拟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19188546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python dice rolling simulation
提问by Tim shaw
I'm having trouble with a code where I need to roll a six-sided die 1000 times and then return a list of how many times each number on the die was rolled.
我在使用代码时遇到问题,我需要将六面骰子掷 1000 次,然后返回骰子上每个数字掷出多少次的列表。
The code runs just fine and I can get a list at the end, but my list keeps having 0 in place of four so it appears that my function is not keeping tabs on the number 4 being rolled or it's not being rolled at all.
代码运行得很好,我可以在最后得到一个列表,但我的列表一直用 0 代替 4,所以看起来我的函数没有关注正在滚动的数字 4 或者根本没有滚动。
I'm kind of stumped and I thought maybe someone here could help. Any and all help is appreciated.
我有点难住了,我想也许这里有人可以提供帮助。任何和所有的帮助表示赞赏。
Here's my code.
这是我的代码。
def rollDie(number):
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
for i in range(0, number):
roll=int(random.randint(1,6))
if roll == 1:
one = one+1
elif roll == 2:
two = two+1
elif roll == 3:
three = three+1
elif roll == 4:
four == four+1
elif roll == 5:
five = five+1
elif roll == 6:
six = six+1
return [one,two,three,four,five,six]
采纳答案by Martijn Pieters
You have a small typo; you are testing for equality, not assigning:
你有一个小错字;您正在测试相等性,而不是分配:
four == four+1
should be:
应该:
four = four+1
However, you already have a number between 1 and 6, why not make that into an index into the results list? That way you don't have to use so many if
statements. Keep your data out of your variable names:
但是,您已经有了 1 到 6 之间的数字,为什么不将其作为结果列表的索引呢?这样你就不必使用这么多if
语句。将您的数据排除在变量名之外:
def rollDie(number):
counts = [0] * 6
for i in range(number):
roll = random.randint(1,6)
counts[roll - 1] += 1
return counts
回答by user278064
You should do random.randint(1, 7)
, otherwise you will never get a 6.
你应该这样做random.randint(1, 7)
,否则你永远不会得到 6。
...
roll = random.randint(1, 7)
回答by steveha
I can't improve on Martijn Pieters's answer. :-) But this problem can be more conveniently solved using a list.
我无法改进 Martijn Pieters 的回答。:-) 但是使用列表可以更方便地解决这个问题。
import random
def rollDie(number):
# create a list with 7 values; we will only use the top six
rolls = [0, 0, 0, 0, 0, 0, 0]
for i in range(0, number):
roll=int(random.randint(1,6))
rolls[roll] += 1
return rolls
if __name__ == "__main__":
result = rollDie(1000)
print(result[1:]) # print only the indices from 1 to 6
And, this is a little bit tricky, but here is a better way to create a list of 7 entries all set to zero:
而且,这有点棘手,但这里有一个更好的方法来创建 7 个条目的列表,所有条目都设置为零:
rolls = [0] * 7
Why count the zeros yourself? It's easier to just make Python do the work for you. :-)
为什么要自己数零?让 Python 为您完成工作更容易。:-)
EDIT: The list is length 7 because we want to use indices 1 through 6. There is also a position 0 in the list, but we don't use it.
编辑:列表长度为 7,因为我们要使用索引 1 到 6。列表中还有一个位置 0,但我们不使用它。
Another way to do it is to map the dice rolls onto indices. It's a pretty simple mapping: just subtract 1. So, a die roll of 1 would go into index 0 of the list, a die roll of 2 would go into index 1, and so on. Now we will use every position in the list.
另一种方法是将骰子卷映射到索引上。这是一个非常简单的映射:只需减去 1。因此,掷骰子为 1 将进入列表的索引 0,掷骰子为 2 将进入索引 1,依此类推。现在我们将使用列表中的每个位置。
Here's that version:
这是那个版本:
import random
def rollDie(number):
rolls = [0] * 6
for i in range(0, number):
roll=int(random.randint(1,6))
rolls[roll - 1] += 1
return rolls
if __name__ == "__main__":
result = rollDie(1000)
print(result)
回答by dedrick
import random
def dice():
print random.randint(1,6)
dice()