1

私は現在、「Learning Python The Hard Way」という本を読んでいて、簡単なゲームを作ろうとしています。このゲームでは、ある部屋のアイテム「懐中電灯」を拾って、別の部屋に行けるようにしたいです。ただし、機能させることはできません:-(

問題は、同じリストを複数の関数に渡すにはどうすればよいでしょうか。いろいろ入れられるようにしたいです。

自分で 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)
4

1 に答える 1

3

自分自身でpick()関数を呼び出そうとしましたが、関数にリストを提供しているのに、「TypeERROR:'str'は呼び出せません」というメッセージが表示され続けますか?

ここでの問題は、次の行にあることです。

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

新しい値(str)にバインドpickして、関数を参照しないようにします。次のように変更します。

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