我必须制作一个命令行游戏,它接受monster1和2的Affinity | HP | AD并与其进行战斗,同时包含Affinity优势,例如
如果monster1的亲和力是地球,而且怪物2的亲和力比每次命中的*怪物1的AD低20%,并且每次命中的怪物2的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)
然而,一旦我这样做,我就会遇到这个恼人的错误
======= 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.
不确定从哪里开始..我已经看了一遍,并没有真正可靠的答案与列表的乘法无关。 帮助:D
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
>>>
我没有得到最后一个printNow()。我想打印AD1以查看AD1是否正确添加了20%。
return
printNow(AD1)
答案 0 :(得分:0)
您收到此错误是因为您的所有HP
和AD
都是字符串,而不是整数。将字符串乘以int时,您将获得该字符串的多个副本:
>>> "abc" * 3
abcabcabc
由于您无法在逻辑上将字符串乘以0.2,因此会出现错误。要修复它,只需使用内置的int()
函数,如下所示:
HP1 = int(monster1[x+1:y])
作为旁注,我想向您介绍str.split()
功能。只需执行
Affinity1, HP1, AD1 = monster1.split("|")
你已经完成了所有的设置。在任何计算中使用它们之前,只需在需要它的变量上调用int()
。
答案 1 :(得分:0)
您的问题是您尝试将字符串乘以浮点数(AD1 * .20)。
如果启动python解释器,请执行以下操作:
>>> ad = "10"
>>> print ad *.5 + ad
你会得到
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
您想将AD1转换为浮点数:
print float(ad) *.5 + float(ad)
其他一些想法:
我稍微使用了你的代码并想出了以下内容:
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])