我目前正在閱讀「學習Python艱難的方式」一書,並且我正在嘗試做一個簡單的遊戲。在這個遊戲中,我希望能夠在一個房間裏拿起「手電筒」項目,以便能夠進入另一個房間。但是,我可以,但是,它不能使它工作:-(使用列表來收集項目
所以問題是,我如何通過幾個函數攜帶相同的列表,我怎麼把它放在它?我想能夠把多件事情在它
我試着撥打籤()函數,它的自我之內,但要得到一個「類型錯誤:‘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)
完整的TraceBack在這裏很有用。 –
我從這個問題中刪除了'loops'標記,因爲你實際上沒有循環,但你可能需要一個循環。即使沒有在本身重新定義「pick」,遞歸可能不是你想要的。 – geoffspear