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)

This works fine for me:

import random
print random.randint(1, 6)

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()

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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.