[英]The error was:can't multiply sequence by non-int of type 'float'
i have to make a command line game that takes in monster1 and 2's Affinity|HP|AD and battles them while including the Affinity advantages, for example 例如,我必须制作一个包含Monster1和2的Affinity | HP | AD并与之战斗的命令行游戏,同时包括Affinity优势
If monster1's affinity is Earth and monster2's affinity is Fire than * monster1's AD by 20% per hit and lower monster2's AD by 20% per hit 如果Monster1的亲和力是地球,而Monster2的亲和力是火,则* * Monster1的AD每次命中降低20%,Monster2的AD每次命中降低20%
def getmonsterData():
global Affinity1
global Affinity2
global HP1
global HP2
global AD1
global AD2
monster1 = raw_input("Enter the Monster 1's Affinity|HP|AD> ")
x = monster1.index('|')
y = monster1.index('|',x+1)
Affinity1 = monster1[:x]
HP1 = monster1[x+1:y]
AD1 = monster1[y+1:]
printNow(Affinity1+"|"+HP1+"|"+AD1)
monster2 = raw_input("Enter the Monster 2's Affinity|HP|AD> ")
x = monster2.index('|')
y = monster2.index('|',x+1)
Affinity2 = monster2[:x]
HP2 = monster2[x+1:y]
AD2 = monster2[y+1:]
printNow(Affinity2+"|"+HP2+"|"+AD2)
battleNow();
def battleNow():
global Affinity1
global Affinity2
global HP1
global HP2
global AD1
global AD2
if (Affinity1 == Affinity2):
printNow("There are no affinity advantages in this battle")
if (Affinity1 == ("Earth") and Affinity2 == ("Fire")):
AD1 = (AD1*.20)+AD1 #THIS IS SUPPOSED TO RETURN AD1(MONSTER 1'S AD) TO THE INCREASED 20%
printNow("Monster 1 has an affinity advantage over Monster 2")
return
printNow(AD1)
However, once i do this i get this annoying error 但是,一旦我这样做,我会收到这个烦人的错误
======= Loading Progam =======
Enter the Monster 1's Affinity|HP|AD> Earth|55|1
Earth|55|1
Enter the Monster 2's Affinity|HP|AD> Fire|52|1
Fire|52|1
The error was:can't multiply sequence by non-int of type 'float'
Inappropriate argument type.
not sure where to go from this.. i've looked all over and no real solid answers that didnt have to do with multiplying a list. 不知道从哪里去..我到处都看了,没有真正可靠的答案与增加列表无关。 help :D
帮助:D
EDIITTTTTT: after changing my HP1/2 and AD1/2 to int(), i get no error but only recieve the output of EDIITTTTTT:将我的HP1 / 2和AD1 / 2更改为int()之后,我没有收到任何错误,但只收到了输出
======= Loading Progam =======
Enter the Monster 1's Affinity|HP|AD> Earth|22|1
Enter the Monster 2's Affinity|HP|AD> Fire|25|1
Monster 1 has an affinity advantage over Monster 2
>>>
i don't get that last printNow(). 我没有最后一个printNow()。 i wanted to print the AD1 to see if the 20% increase was added to AD1 properly.
我想打印AD1以查看是否将20%的增量正确添加到AD1中。
return
printNow(AD1)
You are getting this error because all your HP
s and AD
s are strings, not ints. 之所以会出现此错误,是因为所有
HP
和AD
都是字符串,而不是整数。 When you multiply a string by an int, you get multiple copies of that string: 将字符串乘以int时,将获得该字符串的多个副本:
>>> "abc" * 3
abcabcabc
Since you can't logically multiply a string by 0.2, you get the error. 由于无法将字符串逻辑上乘以0.2,因此会出现错误。 To fix it, just use the built-in
int()
function like so: 要修复它,只需使用内置的
int()
函数,如下所示:
HP1 = int(monster1[x+1:y])
As a side note, I'd like to introduce you to the str.split()
function. 作为附带说明,我想向您介绍
str.split()
函数。 Instead of calculating string indexes and trying to parse your input that way, simply do 无需计算字符串索引并尝试以这种方式解析输入,只需执行
Affinity1, HP1, AD1 = monster1.split("|")
and you're all set. 你们都准备好了。 Just call
int()
on the variables that require it before using them in any calculations. 在任何计算中使用它们之前,只需对需要它的变量调用
int()
。
Your problem is that you're trying to multiply a string by a float (AD1 * .20). 您的问题是,您试图将字符串乘以浮点数(AD1 * .20)。
If you fire up the python interpreter, and do the following: 如果启动python解释器,请执行以下操作:
>>> ad = "10"
>>> print ad *.5 + ad
You'll get 你会得到
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
You want to convert AD1 to a float: 您要将AD1转换为浮点数:
print float(ad) *.5 + float(ad)
Couple of other thoughts: 其他一些想法:
I played with your code a bit and came up with the following: 我玩了一些您的代码,并提出了以下建议:
class Monster():
"""Define monster characteristics
"""
def __init__(self):
pass
def getMonsterData(self):
monster = raw_input("Enter the Monster's Affinity|HP|AD> ")
self.loadMonsterData(monster)
def loadMonsterData(self, monsterData):
self.affinity, self.hp, self.ad = monsterData.split('|') # not safe
print("{}|{}|{}".format(self.affinity, self.hp, self.ad))
def battleNow(monster1, monster2):
"""Make 2 monsters fight it out
"""
if (monster1.affinity == monster2.affinity):
printNow("There are no affinity advantages in this battle")
if (monster1.affinity == "Earth" and monster2.affinity == "Fire"):
ad = float(monster1.ad)
monster1.ad = ad * .2 + ad #THIS IS SUPPOSED TO RETURN AD1(MONSTER 1'S AD) TO THE INCREASED 20%
print("Monster 1 has an affinity advantage ({}) over Monster 2 ({})".format(monster1.ad, monster2.ad))
return
print(monster1.ad)
if __name__ == '__main__':
monsters = []
monsters.append(Monster())
monsters.append(Monster())
monsters[0].loadMonsterData("Earth|100|10")
monsters[1].loadMonsterData("Fire|50|5")
battleNow(monsters[0], monsters[1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.