我在执行函数TypeError时遇到了这个错误:“dict”对象不是callab

2024-06-26 08:18:49 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能帮我吗?我是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'

Tags: ofinforinputreturnifvalresult
2条回答

正如错误所说,GoodTeam是一个dict,但您正在尝试调用它。我想你的意思是调用它的items方法:

for k, val in GoodTeam.items():

坏队也是如此。在

注意,您还有其他错误;您使用的是string format方法,但没有给它任何实际格式化的内容。在

那些括号是不必要的。您打算使用.items(),它允许您迭代字典的键和值:

for k, val in GoodTeam.items():
    # your code

您还应该为EvilTeam复制此更改。在

相关问题 更多 >