2

I'm extremely new to Python (as in this is the first script I've written), and I'm just messing around trying to make something that will work.

I've written the following:

# Roll the Dice

from random import randint

x = randint.uniform(1, 6)
y = randint.uniform(1, 6)

print str(x + y)

This should simply return any integer between 2 and 12, but I'm getting the following error message when I try to run it:

Traceback (most recent call last):
  File "C:/FilePath/Python Testing.py", line 5, in <module>
    x = randint.uniform(1, 6)
AttributeError: 'function' object has no attribute 'uniform'

I feel like this is a super easy script and shouldn't be failing, but since I'm so new to this I don't even know where to begin troubleshooting. I found this SO question that is similar, but the resolution does not fit my problem (or so I think).

I'm using Python 2.7.12 via PyCharm 2016.1.4

Any help is appreciated!

3
  • 1
    It's random.uniform, not randint.uniform. You'll have to change your import to import random, or from random import uniform and then you can use the unqualified name. Commented Jul 6, 2016 at 18:55
  • yep...figured it was something stupid like that...thank you very much Commented Jul 6, 2016 at 18:58
  • You don't want uniform to simulate dice rolls, since it returns floats. Commented Jul 6, 2016 at 19:15

2 Answers 2

2

You are mixing up modules and functions. randint is a fuction in the random module, as is uniform. Instead of loading just the randint function, load the whole module. See https://docs.python.org/2/library/random.html for more information

# Roll the Dice

import random

x = random.randint(1, 6)
y = random.randint(1, 6)

print str(x + y)

x = random.uniform(1, 6)
y = random.uniform(1, 6)

print str(x + y)
Sign up to request clarification or add additional context in comments.

Comments

1

uniform and randint are both functions* defined in the random module.

from random import uniform
x = uniform(1, 6)

* Not quite; there is a module-level global RNG whose methods are accessible as module-level names.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.