Search code examples
pythonpython-3.xattributeerror

Attribute Error: 'int' object has no attribute 'choice'


I am writing a python program and I get this error:

Traceback (most recent call last):
File "C:\Users\Joe\SkyDrive\Documents\Python Project\Python\Forest path.py", line 28, in   <module>
random = random.choice(accuracy)
AttributeError: 'int' object has no attribute 'choice'

Here is a bit of the code it is referring to:

while health_1 > 0 and health > 0 and stamina > 0:
    random = random.choice(accuracy)
    if random != "0":
        print("\n\n", random)
        print("\nYou manage to hit the creature for", dmg, "damage!")
        health_1 -= dmg
        stamina -= stam_loss
        print("The creature now has", health_1, "health")
        print("\nThe creature hits you for 1 damage!")
        health -= 1
        print("Health:", health, "Stamina:", stamina,) 

It does the random module once and then generates the error Any help appreciated.


Solution

  • random = random.choice(accuracy)
    

    You are getting an int value in the first iteration and storing it in random, which is the module's name. Now, the random variable, shadows the random module. The best fix would be to use, some other variable name instead of random.