Python 'builtin_function_or_method'对象没有属性'__getitem__

3 投票
3 回答
24684 浏览
提问于 2025-04-17 22:24

我正在尝试测试一些东西,以便制作一个Python文字冒险游戏,但它没有正常工作。这里是我的代码:

calories = [3]

fooland= ("fooland")
area=fooland
joint= ("joint")

while area=="fooland":
talk=raw_input("Where to go?")

if talk==joint:
    area=joint

else:
    print "You cant do that!"


while area=="joint":
order=raw_input("What to order?")

if order=="fries":
    print "You eat like a BAWS"
    calories.append[2]

else:
    print "You cant eat that, but here is some grease!"
    calories.append[6]

 if [calories < 10]:
print "YOU ARE FAT"

但是我遇到了这个错误:

'builtin_function_or_method' object has no attribute '__getitem__'

我哪里出错了,应该怎么修复呢?

3 个回答

0

你需要做的是:

    if (int(calories) < 10):
      print "YOU ARE FAT"
1

我也遇到过这个问题,后来发现原因是我在调用函数的时候把 [] 写错了:

 f.pop[para1]   // wrong 
 f.pop(para1)   // correct    

只需要把 [] 改成 (),程序就能正常运行了。

4

要调用一个方法(比如 append),你需要把参数放在()里,而不是[]里。

撰写回答