我正在为小型游戏创建一个回合制战斗系统,我收到了错误
TypeError:类型'类型'的对象没有len()
在该行:
AImove = random.choice(AImove)
作为参考,AImove
变量的设置如下:
AImove = [1,2]
我尝试将AImove = int
和AImove = str
添加到代码中,但我收到的错误与之前相同或"TypeError: object of type 'int' has no len()"
使用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
答案 0 :(得分:1)
您立即将AImove
替换为random.choice
的结果,之后是1
或2
,并在下一次迭代中尝试{{1} }(或random.choice(1)
)解释了您所看到的例外情况。
您可以在那里使用另一个变量名称:
2