1

我目前正在阅读“Learning Python The Hard Way”一书,并且正在尝试制作一个简单的游戏。在这个游戏中,我希望能够在一个房间里拿起物品“手电筒”,以便能够进入另一个房间。但是,我不能让它工作:-(

那么问题来了,如何通过多个函数携带同一个列表,又如何将东西放入其中?我希望能够在其中放入多个东西。

我尝试在其中调用 pick() 函数,但不断收到“TypeERROR: 'str' is not callable, 尽管我为我的函数提供了一个列表?

希望你能帮助我,谢谢:-)

代码:

def start(bag):
        print "You have entered a dark room"
        print "You can only see one door"
        print "Do you want to enter?"

        answer = raw_input(">")

        if answer == "yes":
            light_room(bag)
        elif answer == "no":
            print "You descidede to go home and cry!"
            exit()
        else:
            dead("That is not how we play!")

def light_room(bag):
    print "WOW, this room is amazing! You see magazines, cans of ass and a flashlight"
    print "What do you pick up?"
    print "1. Magazine"
    print "2. Cans of ass"
    print "3. Flashlight"

    pick(bag)

def pick(bag):    
    pick = raw_input(">")

    if int(pick) == 1:
        bag.append("Magazine")
        print "Your bag now contains: \n %r \n" % bag
    elif int(pick) == 2:
        bag.append("Can of ass")
        print "Your bag now contains: \n %r \n" % bag
    elif int(pick) == 3:
        bag.append("Flashlight")
        print "Your bag now contains: \n %r \n" % bag                    
    else:
        print "You are dead!"
        exit()

def start_bag(bag):
    if "flashlight" in bag:
        print "You have entered a dark room"
        print "But your flashlight allows you to see a secret door"
        print "Do you want to enter the 'secret' door og the 'same' door as before?"

        answer = raw_input(">")

        if answer == "secret":
            secret_room()
        elif answer == "same":
            dead("A rock hit your face!")
        else:
            print "Just doing your own thing! You got lost and died!"
            exit()
    else:
        start(bag)

def secret_room():
    print "Exciting!"
    exit() 

def dead(why):
    print why, "You suck!"
    exit()

bag = []
start(bag)
4

1 回答 1

3

我尝试在其中调用 pick() 函数,但不断收到“TypeERROR: 'str' is not callable, 尽管我为我的函数提供了一个列表?

这里的问题是在这一行:

def pick(bag):    
    pick = raw_input(">")

您绑定pick到一个新值(str),因此它不再引用函数。将其更改为:

def pick(bag):    
    picked = raw_input(">")
于 2012-09-06T14:19:53.740 回答