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

Sachin Kanchan
Sachin Kanchan
564 Points

AttributeError: module 'random' has no attribute 'randint'

I ran this code here on TeamTreeHouse work-space few days ago and it was fine. Now I am getting this error:- "AttributeError: module 'random' has no attribute 'randint'
Why? Is this a teamtreehouse bug?

import random

def game():

    # generate random number between 1 & 20
    secret_num = int(input("Enter a number between 1 & 20 for me to guess: "))

    # computer guesses numbers to display to user    
    guess = random.randint(1, 20)            
    guesses = []

    while len(guesses) < 5:

        if guess==secret_num:
            print("Ya'ay! I got it. The number is {}".format(secret_num))
            break

        elif guess!=secret_num:

            # if it doesn't get correct answer in 1st attempt, it will ask for feedback
            # type "l" if your secret_num is less than computer's guess
            # type "h" if your secret_num is more than computer's guess

            print("Is it lower than or higher than {}".format(guess))
            user_input= input("")
            if user_input=="l":
                guess = random.randint(1, guess)
                print(guess)
            elif user_input=="h":
                guess = random.randint(guess, 20)
                print(guess)
        guesses.append(guess)

    else:
        print("I lost. Your number was: {}".format(secret_num))
game()

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Your code compiles and runs fine for me. Have you tried making a new workspace and then copy/pasting the code you have here into a new file? Then recompile and run it. I know that a day or two ago there was some emergency maintenance on the workspaces system. This would be the first thing to try. But I copied and pasted your code into a brand new python file and it compiled and ran just fine :smiley:

Sachin Kanchan
Sachin Kanchan
564 Points

Thanks heaps Jen :) I created a new workspace, and a new file. But still same issue. Will mail TTH support.

Sachin Kanchan
Sachin Kanchan
564 Points

HI Jennifer, I realized the problem. I named my file "random.py" Hence the problem. I renamed it to rand_num.py and it works fine now.

The issue was when the code tried to import random module, it tried to import itself. :D LOL