如何解决“类型错误:不能将序列乘以” float”类型的非整数?

时间:2020-02-25 22:08:16

标签: python python-3.x tkinter

我正在制作一个Pokemon Battle Simulator(某种程度上类似于Pokemon Showdown),并且在创建战斗步骤(计算伤害)的过程中,发生错误TypeError: can't multiply sequence by non-int of type 'float'

这是我的代码:

from tkinter import *
from tkinter import ttk


def fight():
    moveused1 = moveset1.get()
    moveused2 = moveset2.get()
    if moveused1 == "Tackle":
        movetype1 = "Normal"
        moverange1 = "Physical"
        power1 = "40"
    if moveused2 == "Tackle":
        movetype2 = "Normal"
        moverange2 = "Physical"
        power2 = "40"
    if moverange1 == "Physical":
       Pokemon1Damage = ((2 * 100 / 5 + 2) * power1 * Attack1 / Defence1 / 50 + 2)
    if moverange1 == "Special":
       Pokemon1Damage = ((2 * 100 / 5 + 2) * power2 * SpAttack2 / SpDefence2 / 50 + 2)

def start():
    global moveset1
    global moveset2
    global HP1
    global Attack1
    global SpAttack1
    global Defence1
    global SpDefence1
    global HP2
    global Attack2
    global SpAttack2 
    global Defence2
    global SpDefence2
    setup.withdraw()
    battle = Tk()
    battle.title(pokemon1 + " VS " + pokemon2)
    battle.geometry("600x400")
    battle.iconbitmap("icon.ico")
    if pokemon1 == "Bulbasaur":
       HP1 = "231"
       Attack1 = "134"
       SpAttack1 = "166"
       Defence1 = "134"
       SpDefence1 = "166"
    if pokemon2 == "Bulbasaur":
       HP2 = "231"
       Attack2 = "134"
       SpAttack2 = "166"
       Defence2 = "134"
       SpDefence2 = "166"
    move1 = move1select.get()
    move2 = move2select.get()
    move3 = move3select.get()
    move4 = move4select.get()
    Label(battle, text="Battle").pack()
    Label(battle, text="").pack()
    Label(battle, text=pokemon1 + "'s Move").pack()
    moveset1 = ttk.Combobox(battle, state="readonly", values=(move1, move2, move3, move4))
    moveset1.current(0)
    moveset1.pack()
    move5 = move5select.get()
    move6 = move6select.get()
    move7 = move7select.get()
    move8 = move8select.get()
    Label(battle, text="").pack()
    Label(battle, text=pokemon2 + "'s Move").pack()
    moveset2 = ttk.Combobox(battle, state="readonly", values=(move5, move6, move7, move8))
    moveset2.current(0)
    moveset2.pack()
    Button(battle, text="Fight", command=fight).pack()

编辑:这是完整的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\colli\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\colli\Desktop\Pokemon Battle Buddy\Pokemon Battle Buddy.py", line 19, in fight
    Pokemon1Damage = ((2 * 100 / 5 + 2) * power1 * Attack1 / Defence1 / 50 + 2)
TypeError: can't multiply sequence by non-int of type 'float'

1 个答案:

答案 0 :(得分:5)

您正在将字符串乘以浮点数。 在这里:

if moverange1 == "Physical":
   Pokemon1Damage = ((2 * 100 / 5 + 2) * power1 * Attack1 / Defence1 / 50 + 2)
if moverange1 == "Special":
   Pokemon1Damage = ((2 * 100 / 5 + 2) * power2 * SpAttack2 / SpDefence2 / 50 + 2)

power1power2Attack1Defence1SpAttack2SpDefence2都是字符串。 您应该在代码中定义整数的地方替换它们。