Jul-31-2019, 01:42 AM
First, a list would be a better way to store the results. If you have
Then you can check that
counts = [0] * 11
, then you can avoid the if/elif chain. I would also eliminate looping through twice:for num in range(1000): counts[random.randint(1, 10)] += 1Then a simple list comprehension gets the percentages:
percentages = [count / 1000 for count in counts]Note that your ability to use list_length in a function is not inexplicable. If Python can't find the value in the function scope, it will check the module/global scope for it. That is, as long as you haven't done an assignment to that name, which flags the name as local scope to Python.
Then you can check that
sum(percentages) == 1
. I expect it will be wrong due to the minor inaccuracies of floating point math, but it should be close.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures