Python 3.x 开始Python-变量不是“;储蓄?“;
上个月,我在高中开始了一门Python电脑游戏编程课程,目前的项目是编写一个非常基础的文本冒险游戏。虽然我遇到了一些问题,但我还是设法通过阅读Python教程或阅读过去有关Stackoverflow的问题来找到解决方案。然而,在过去的两天里,我一直被困在一个问题上,我无法解决这个问题。我一直在尝试将一个变量更改为一个输入,它通常可以无缝地工作。然而,在这种情况下,它似乎有一个相当短的内存变量被改变。同时也为混乱的代码和在论坛上发布的错误提前道歉;我对两者都是新手Python 3.x 开始Python-变量不是“;储蓄?“;,python-3.x,Python 3.x,上个月,我在高中开始了一门Python电脑游戏编程课程,目前的项目是编写一个非常基础的文本冒险游戏。虽然我遇到了一些问题,但我还是设法通过阅读Python教程或阅读过去有关Stackoverflow的问题来找到解决方案。然而,在过去的两天里,我一直被困在一个问题上,我无法解决这个问题。我一直在尝试将一个变量更改为一个输入,它通常可以无缝地工作。然而,在这种情况下,它似乎有一个相当短的内存变量被改变。同时也为混乱的代码和在论坛上发布的错误提前道歉;我对两者都是新手 #actions for
#actions for room one
def roomOne():
if adultDragon == "1": #later part of puzzle
print ("To be continued...") #change this later
elif clothes == "0" and bread == "0":
print ("You look around the room that you're in. It's very bare, with a tattered wood cabinet on the south wall right next to an opening that leads to the next room.")
time.sleep(3)
action = input ("You should probably find a way to get out of this cave. ") #should be saving variable??
while action != "go south" and action != "south" and action != "cabinet" and action != "open cabinet" and action != "door" and action != "open door":
action = input ("I don't understand. ")
#error here: as soon as input is equal to one of the options, it turns "action" into "look around" WHY? (Look around was a user input earlier in the script... but it should've been changed using the last user input)
#use a variable as a workaround? #it's remembering action from previous script... possible local/global issue? Trying different variable may work? (Edit: tried changing variable name eg. "actionOne" and result was the same. Last user input is not being stored.)
if action == "go south" and action == "south":
print ("You approach the wooden cabinet.")
elif action == "cabinet" and action == "open cabinet":
print ("You can't reach that from here. ")
elif action == "door" and action == "open door":
print ("You don't feel strong enough to leave the room yet. ")
roomOnePuzzle() #prompts for the room's puzzle
def roomOnePuzzle(): #Thinks action is what it was previously (see above)
print (action) #only for testing purposes to find out what it thinks "action" is at this point... appears to think it is the previous user input instead of current input.
我应该发布整个代码吗?以上是我认为与错误相关的1/3。。。正如你所看到的,我还没有深入到游戏中。我讨厌寻求帮助,但我开始担心在任务到期日即将到来的情况下陷入如此长的僵局 怎么可能:
if action == "go south" and action == "south":
是否曾经评估过True
?相反,您需要:
if action == "go south" or action == "south":
# ^ note
或者,更像蟒蛇:
if action in ('go south', 'south'):
您也可以在循环中使用此选项:
while action not in ('go south', 'south', ...):
更好的方法是使用字典:
actions = {'go south': "You approach the wooden cabinet.",
'south': "You approach the wooden cabinet.",
...}
action = input(...).lower()
while action not in actions:
action = input(...).lower()
print(actions[action]) # no need for if
room_one_puzzle(action) # note PEP-8 name and explicit argument
最后,不要依赖作用域来访问所需的值,重构显式参数和返回值;例如,
衣服
和面包
可以位于传递给每个函数的库存
字典中 谢谢你的帮助!我不完全理解为什么“如果行动==”向南走“和行动==”向南“:”有效,但它确实有效。。。早些时候,我尝试使用OR(逻辑性更强),但遇到了一些问题。一个不同的stackoverflow回答的问题建议我使用AND,然后使用AND,并在代码的其他几个部分中使用。然而,我更喜欢你做这件事的方式,尽管我们还没有在课堂上学会。我将尝试修改您明天编写的代码,看看它是否修复了任何问题。:)@emptamar“南部”==“南部”和“南部”==“向南”
评估False
。假设action
是一个字符串,它不能同时等于两个不同的字符串文本。这似乎解决了我遇到的问题。再次感谢!