我最近开始苦学Python。我在练习36,我们设计自己的文本冒险游戏。我希望用户通过不同的房间收集物品,以便稍后在最后一个房间的老板身上使用。但我想不出当我换房间的时候,我们怎么能一直在同一张单子上加。这是我到目前为止为该部分(我剪下描述文字)。。。你知道吗
def add_item():
backpack = []
i = 0
while 1:
i += 1
item = raw_input(" > ")
if item == '':
break
backpack.append(item)
print "\nAh, the %s, let us hope this serves you well." % item
print "This is the inventory you have acquired so far..."
print backpack
def dizzygas_hallway():
print "If so which item do you choose? (cloak or pendant)"
add_item()
def dark_laboratory():
print "Which item do you take? (book, potion or sword)\n"
add_item()
print "You exit the only door in sight..."
dizzygas_hallway()
我想你很接近了,换几句话就行了。否则,每次您要添加一个项目时,都会创建一个新的空列表。你知道吗
可以在处理列表的函数中使用
global
。你知道吗不过,如果您学会用OOP编写代码会更好
相关问题 更多 >
编程相关推荐