Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

problem with import random

Hello community, I'm having problem with importing random. The error message is: "Attribute error. module 'random' has no attribute 'randint'"

I have made sure to save the file, and I do not type it wrong i console. Thanks for any help!

import random

# generate a random number between 1 and 10
secret_num = random.randint(1, 10)

while True:
    # get a number guess from the player
    guess = int(input("Guess a number between 1 and 10: "))

    # compare guess to secret number
    if guess == secret_num:
        print("You got it! My number was {}".format(secret_num))
        break
    else:
        print("That's not it!")

3 Answers

Is your program named random? Or do you have a program named random.py somewhere? Because random is conflicting with another file named similarly. Try running it in workspaces after renaming similarly named files. I too had named my file random.py and got the same error but after renaming your program ran just fine.

your import statement looks fine. Try This:

from random import randint

secret_num = randint(1, 10)

It works! I made it easy for me by just creating a Workspace and then it worked. I guess the names were conflicting somehow like you said, Sahil.

Thanks both of you for your help!