TypeError:“type”类型的对象没有len()

2024-05-03 14:09:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个回合为一个小游戏战斗系统,我收到了错误

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

在线:

AImove = random.choice(AImove)

作为参考,AImove变量的设置如下:

^{pr2}$

我试图在代码中添加AImove = intAImove = str,但我要么收到与之前相同的错误,要么是{}

使用AImove的函数如下所示,如有任何反馈,将不胜感激。在

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

Tags: andoftoloopifresponse错误random
1条回答
网友
1楼 · 发布于 2024-05-03 14:09:23

您立即将AImove替换为random.choice的结果,然后它要么是1,要么是{},在下一次迭代中,您尝试random.choice(1)(或2),这解释了您所看到的异常。在

您只需在此处使用另一个变量名:

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

相关问题 更多 >