使用列表收集物品

时间:2012-09-06 14:14:23

标签: python list

我目前正在阅读“学习Python艰难之路”这本书,我正在尝试制作一款简单的游戏。在这个游戏中,我希望能够在一个房间拿起“手电筒”项目,以便能够进入另一个房间。但是,我不能让它发挥作用: - (

所以问题是,我如何通过几个函数携带相同的列表,以及如何将其放入其中?我希望能够在其中加入多种内容。

我试图在其中调用pick()函数,但是继续得到一个“TypeERROR:'str'不可调用,虽然我正在为我的函数提供一个列表?

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

代码:

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)

1 个答案:

答案 0 :(得分:3)

  

我试图在其中调用pick()函数,但是继续得到一个“TypeERROR:'str'不可调用,虽然我正在为我的函数提供一个列表?

问题在于这一行:

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

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

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