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)

Recommended Answers

[QUOTE=pi_lord12;1420529]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!

[code]
import random

int1=random.randint(1, 6)

Jump to Post

All 5 Replies

Re: random module/randint attribute 80 80

This works fine for me:

import random
print random.randint(1, 6)
Re: random module/randint attribute 80 80

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()
Re: random module/randint attribute 80 80

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.

Re: random module/randint attribute 80 80

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.

Re: random module/randint attribute 80 80

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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.