user3092741
user3092741

Reputation: 69

'AttributeError: 'module' object has no attribute 'randomVariable''

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?

Upvotes: 0

Views: 1389

Answers (2)

Mohamed Abd El Raouf
Mohamed Abd El Raouf

Reputation: 928

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()

Upvotes: 0

NPE
NPE

Reputation: 500307

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).

Upvotes: 1

Related Questions