有人能帮我吗?我是python和编程的初学者。非常感谢。 我得到了这个TypeError:“dict”对象在我执行这个函数时不可调用。在
def goodVsEvil(good, evil):
GoodTeam = {'Hobbits':1, 'Men':2, 'Elves':3, 'Dwarves':3, 'Eagles':4, 'Wizards':10}
EvilTeam = {'Orcs':1, 'Men':2, 'Wargs':2, 'Goblins':2, 'Uruk Hai':3, 'Trolls':5, 'Wizards':10}
Gworth = 0
Eworth = 0
for k, val in GoodTeam():
Input = raw_input ('How many of {0} : ')
Gworth = Gworth + int(Input) * val
for k, val in EvilTeam():
inp = raw_input ('How many of {0} : ')
Eworth = Eworth + int(inp) * val
if Gworth > Eworth:
return 'Battle Result: Good triumphs over Evil'
if Eworth > Gworth:
return 'Battle Result: Evil eradicates all trace of Good'
if Eworth == Gworth:
return 'Battle Result: No victor on this battle field'
正如错误所说,
GoodTeam
是一个dict,但您正在尝试调用它。我想你的意思是调用它的items
方法:坏队也是如此。在
注意,您还有其他错误;您使用的是string format方法,但没有给它任何实际格式化的内容。在
那些括号是不必要的。您打算使用
.items()
,它允许您迭代字典的键和值:您还应该为
EvilTeam
复制此更改。在相关问题 更多 >
编程相关推荐