Python Python Basics (2015) Number Game App Number Game Refinement

Jarlei Sassi
Jarlei Sassi
5,180 Points

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'

5 Answers

Jarlei Sassi
Jarlei Sassi
5,180 Points
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
Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 56,145 Points

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.

Jarlei Sassi
Jarlei Sassi
5,180 Points

No, same problem.

The error is in the first line :/

Steve Hunter
Steve Hunter
Treehouse Moderator 56,145 Points

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

Steve Hunter
Steve Hunter
Treehouse Moderator 56,145 Points

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

Jarlei Sassi
Jarlei Sassi
5,180 Points

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

Jarlei Sassi
Jarlei Sassi
5,180 Points

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