lg99
lg99

Reputation: 71

TypeError: object of type 'type' has no len()

I am creating a turn based combat system for a small game and I am receiving the error

TypeError: object of type 'type' has no len()

in the line:

AImove = random.choice(AImove)

For reference, the AImove variable is set out as follows:

AImove = [1,2]

I have tried to add AImove = int and AImove = str into the code but I either receive the same error as before or "TypeError: object of type 'int' has no len()"

The function where AImove is used appears below, any feedback would be appreciated.

def combat(health, turn, loop, gold, AImove):

    enemy_health = (random.choice(random_enemy_Health))
    enemy_attack = (random.choice(random_enemy_Attack))
    print("\nYou are fighting a" ,random.choice(enemies), "with an attack amount of" ,enemy_attack, "and a health amount of" ,enemy_health,".")
    while health > 0 and enemy_health > 0:
        if turn == 1:
            while loop == False:
                print("\nDo you want to attack or flee? Type '1' to attack and '2' to flee.")
                response=input()
                if response == "1":
                        enemy_health = enemy_health - attack
                        print("You attacked!")
                        loop = True                       
                elif response == "2":
                    hub_travel()
                    print ("You fled the battle, come back once you are stronger!")
                    loop = True
                else:
                    print ("Invalid number, try again")
                    continue
            turn = 2                                                    

        if turn == 2:
                AImove = random.choice(AImove)
                if AImove == 1:
                    print ("Enemy attacked!")
                    health = health - enemy_attack
                if AImove == 2:
                    print("Enemy missed!")
                turn = 1                                                    
                continue

Upvotes: 0

Views: 6767

Answers (1)

MSeifert
MSeifert

Reputation: 152587

You immediatly replace AImove with the result of random.choice, afterwards it's either 1 or 2 and in the next iteration you try random.choice(1) (or 2) which explains the exception you've seen.

You could simply use another variable name there:

# ...
AImove_this_turn = random.choice(AImove)
if AImove_this_turn == 1:
    print ("Enemy attacked!")
    health = health - enemy_attack
if AImove_this_turn == 2:
    print("Enemy missed!")
turn = 1                                                    
continue

Upvotes: 1

Related Questions