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 Python Basics (2015) Number Game App Number Game Refinement

Cannot import random library

Traceback (most recent call last):
File "guess_game.py", line 1, in <module>
import random
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/random.py", line 41, in <module>
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
ImportError: cannot import name 'log'

Can you post your code, please?

Steve.

5 Answers

import random

cont = 1
num = randint(1,10)

print("##### Guess game ####")

while cont < 6:

    try:
        guess = int(input("Enter a number between 1 to 10"))    

    except ValueError:
        print("Entre com um numero")

    cont = cont + 1

Hi there,

Thanks for the code.

I think you need to call randint on random like num = random.randint(1, 10)

Give that a go.

Steve.

No, same problem.

The error is in the first line :/

The error reporter is picking up on something in the random class being used wrongly - the error is within the usage of part of the random class. The error can sometimes appear really unhelpful! Clearly, there's nothing wrong with the random class itself as we haven't touched it!

I ran this code with no error:

import random

cont = 1
num = random.randint(1,10)

print("##### Guess game ####")

while cont < 6:

    try:
        guess = int(input("Enter a number between 1 to 10"))    

    except ValueError:
        print("Entre com um numero")

    cont = cont + 1

I'll try to post a screenshot of the code working - this may take a few attempts, though as I always get it wrong! Now visible - only two attempts at it!!

pic

Might be worth saving your workspace and restarting it to see if that helps. Your code is working OK here, with that small amendment.

I've created a new folder and a new file. Now it works

Very strange - glad you got it fixed!

It's strange because when I ran this code It was running a function from another file. o.O