0

I have got 2 files which are imported to a main file, and are ran.

When I run it I keep getting the error:

dice_sides = random_var.randomVariable()
AttributeError: 'module' object has no attribute 'randomVariable'
>>> 

My code: Main file:

import random, random_var, dice_roll

dice = [4, 6, 12]

def main():
    dice_sides = random_var.randomVariable()
    dice_roll.diceRoll(dice_sides)

main()

Random_var file:

import random, task_one # import modules and file to be used in program

dice = [4, 6, 12] # declare list

def randomVariable(): # function

    try: # tries this expression
        dice_sides = int(input("Please enter the amount of sides you want the dice that is being thrown to have. The sides available are: 4, 6 and 12: "))
    except ValueError: # if user entered anything other than an integer
        print("You did not enter an integer. Program restarting.")
        task_one.main() # restarts, by getting the main function from "task_one" file

    return dice_sides; # returns variable so it can be used in other functions

Dice_roll file:

import random, task_one

dice = [4, 6, 12]
def diceRoll(dice_sides):

    if dice_sides in dice:
        diceThrown = random.randrange(1, dice_sides)
        print(dice_sides, " sided dice, score ", diceThrown)
    else:
        print("Number is invalid. Please choose either 4, 6 or 12. ")
        task_one.main()

What is wrong?

5
  • 1
    Are you trying this in an interactive interpreter? If so, restart it (and make sure you saved the file in your editor). The interpreter caches modules and reuses them when they are imported, so changes aren't picked up. Commented Mar 12, 2014 at 10:52
  • I done that, but it still comes up with the error. Commented Mar 12, 2014 at 10:53
  • I couldn't reproduce the problem on my machine - print dir(random_var) inside the main() function and paste the output Commented Mar 12, 2014 at 10:57
  • ImportError: No module named random_var I even have that file saved inside the same directory as my main file. Commented Mar 12, 2014 at 11:00
  • does random_var have a .py extension? Commented Mar 12, 2014 at 11:04

2 Answers 2

1

The most likely explanation is that random_var isn't what you think it is.

If you are running this in an interactive interpreter, close and restart it to ensure that the module gets reloaded.

Beyond that, make sure you've saved random_var.py, that its file name is correct (including capitalization), and that it's been saved where import random_var is looking for it (i.e. that you haven't ended up with two random_var.py files in different directories).

Sign up to request clarification or add additional context in comments.

Comments

0

You can try:

import random
from random_var import randomVariable
from dice_roll import diceRoll

dice = [4, 6, 12]

def main():
    dice_sides = randomVariable()
    diceRoll(dice_sides)

main()

2 Comments

I tried that, but it just came up with the error ImportError: cannot import name randomVariable
are all files in the same directory?

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.