By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

TypeError: 'module' object is not callable

P: 2
All, I am trying to learn python. The below code is from a book example. I have researched the error:
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "bin/flips.py", line 4, in <module>
  3.     import random
  4.   File "/home/david/bin/random.py", line 10, in <module>
  5.     if random.random() < 0.5:
  6. 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
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import random
  5.  
  6. experiments, flips = 500, 16
  7.  
  8. #count = 0
  9. heads = [0] * (flips + 1)
  10.  
  11. # Perform each experiment.
  12.  
  13. for i in range(experiments):
  14.   count = 0
  15.   # Accumulate the number of heads.
  16.  
  17.   for j in range(len(heads)):
  18.     if random.random() < 0.5: count += 1
  19.   heads[count] += 1
  20.  
  21. # Print the numeric results.
  22.  
  23. print "heads:\n", heads, "\n"
  24.  
  25. # Print the histogram.
  26.  
  27. for j in range(flips + 1):
  28.   print "%2d " % j,
  29.   i = 0
  30.  
  31.   # print a "*" for each 10 occurrences.
  32.  
  33.   while i < heads[j]:
  34.     print "*",
  35.     i += 10
  36.  
  37.   print
Jun 6 '11 #1
Share this Question
Share on Google+
2 Replies


P: 2
Sorry, folks. I finally found the issue. It was starring me straight in the face. I found that I had created a previous python program and used the same name as a python class/module, which I should not have done.
Jun 6 '11 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Good find David. Post again if you have any other issues.
Jun 7 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.