Expand|Select|Wrap|Line Numbers
- Traceback (most recent call last):
- File "bin/flips.py", line 4, in <module>
- import random
- File "/home/david/bin/random.py", line 10, in <module>
- if random.random() < 0.5:
- TypeError: 'module' object is not callable
What I have found is that the code example (below) is referencing the class object (random) and not the function of the class. Does anyone know how to fix this and any additional knowledge of why it is happening would be great.
Code:
Expand|Select|Wrap|Line Numbers
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import random
- experiments, flips = 500, 16
- #count = 0
- heads = [0] * (flips + 1)
- # Perform each experiment.
- for i in range(experiments):
- count = 0
- # Accumulate the number of heads.
- for j in range(len(heads)):
- if random.random() < 0.5: count += 1
- heads[count] += 1
- # Print the numeric results.
- print "heads:\n", heads, "\n"
- # Print the histogram.
- for j in range(flips + 1):
- print "%2d " % j,
- i = 0
- # print a "*" for each 10 occurrences.
- while i < heads[j]:
- print "*",
- i += 10