我想在赋值之前弄清楚如何使用if语句来更改它。这个脚本的重点是在提示问题将刀从桌子上拿下来之前检查刀是否被拿走。这是为了让你可以走回桌子,得到另一个回应,如果你已经采取了。我做错什么了?你知道吗
def table ():
if knife_taken == False:
print "it's an old, brown wooden table, and atop it you find a knife"
print "Will you take the knife or go back?"
knife = raw_input ("> ")
if knife.strip().lower() in ["back", "b", "no"]:
basement2()
elif knife.strip().lower() in ["take knife", "knife", "yes", "k"]:
knife_taken = True
print "You now have the knife, good, you are going to need it"
raw_input()
basement2()
else:
print "I did not understand that."
raw_input()
table()
else:
print "There's nothing on the table"
raw_input()
basement2()
基本上,当您更改函数中的变量时,您会在
local
级别更改它,这意味着当函数结束时,更改将丢失。有两种方法可以解决这个问题,或者使用global
(但这是一种不好的方法)也可以从函数返回刀的状态
并将其存储在变量中,稍后将其作为参数传递回厨房
或者作为额外的一点奖励,你可以把游戏状态存储在字典里。你可以在游戏状态改变时更新字典
相关问题 更多 >
编程相关推荐