954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

random module/randint attribute

Hey, I'm new to Python and wondering about the following bit of code. It tells me that the module random has no attribute randint. I've looked at the documentation and such and I think I'm doing it correctly, but obviously something's wrong. Any ideas? Thanks!

import random

int1=random.randint(1, 6)
int2=random.randint(1, 6)

print "Die one was "+str(int1)
print "Die two was "+str(int2)
print "Your total was "+str(int1+int2)
pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

This works fine for me:

import random
print random.randint(1, 6)
seanbp
Junior Poster
129 posts since Jul 2010
Reputation Points: 20
Solved Threads: 15
 

Hi have a simalar problem but try this bit coding. hope it help you

import random
# make def statement


def gameDice():
   d1= random.randint(1 ,10)
   d2= random.randint(1,10)
   print (' \n' 'number from d1:=', d1, ':number for d2=', d2)
   
for count in range(1,11):
   gameDice()
FAITH2011
Light Poster
29 posts since Dec 2010
Reputation Points: 10
Solved Threads: 2
 

That code runs fine for me. Could you post the error traceback?

BTW: I like how you put a space after print, it's backwards-compatible that way.

seanbp
Junior Poster
129 posts since Jul 2010
Reputation Points: 20
Solved Threads: 15
 

Hey, I'm new to Python and wondering about the following bit of code. It tells me that the module random has no attribute randint. I've looked at the documentation and such and I think I'm doing it correctly, but obviously something's wrong. Any ideas? Thanks!

import random

int1=random.randint(1, 6)
int2=random.randint(1, 6)

print "Die one was "+str(int1)
print "Die two was "+str(int2)
print "Your total was "+str(int1+int2)

Something tells me that you saved one of your programs as random.py. Avoid naming any of your programs with module names.

Python looks into the working directory first for imports. If it finds random.py (your homemade program) there, it doesn't look any further. What you really want is the random.py module file that is in the Lib directory that would be searched later in the PYTHONPATH.

So, if you have a random.py file in your working directory, rename it to something else to avoid this conflict.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Yep, you were right, vegaseat, the name was the problem. I'll keep that in mind in the future. Thanks everyone!

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: