为什么熊(布尔值)不留在原位?
Why doesn't the bear(boolean) remain in its position?
我想为 Zed Shaw 的 LPTHW 练习 35 添加更多功能。脚本 运行 没有崩溃,并允许玩家按照我的意愿重新访问以前的房间。然而,在 bear_room 中,玩家唯一可以通过的方法就是对着熊尖叫,将 bear_moved 布尔值切换为 True。
如果玩家这样做然后倒退到起始房间,我的意图是 bear_moved 布尔值保持在 True 位置,这意味着熊仍然会被移离门重新进入。
当我 运行 脚本时,这并没有发生;第一次进入 bear_room 时,我对着熊尖叫,让它离开了门。然后我退出房间,回到开始的房间。当我回到 bear_room 时,那只熊又神秘地扑通一声趴在门前。
出于这个目的,我将 bear_moved 布尔值放在函数之外——这是我唯一能想到的为程序提供额外功能的方法。
回顾一下,当我退出 bear_room 并重新进入时,为什么熊不移动?我怎样才能实现我想要的功能?
from sys import exit
bear_moved = False
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
if next.isdigit():
if int(next) > 101:
dead("You greedy bastard!")
elif int(next) < 101:
print "Nice, you're not greedy! You win!"
exit(0)
else:
pass
elif 'back' in next or 'backtrack' in next:
bear_room(bear_moved)
else:
print "Type a number!"
gold_room()
def bear_room(bear_moved):
if bear_moved == False:
print "There's a bear in here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
elif bear_moved == True:
print "The bear has moved away from the door."
else:
pass
next = raw_input("> ")
if 'honey' in next:
dead("The looks at you and then slaps your face off.")
elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == False:
bear_moved = True
bear_room(bear_moved)
elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == True:
dead("The bear flies into a rage and chews your leg off.")
elif 'open' in next and bear_moved == True:
gold_room()
elif 'open' in next and bear_moved == False:
dead("The bear, still standing in the way, lunges up and rips your throat out.")
elif 'back' in next or 'backtrack' in next:
start()
else:
print "I got no idea what this means."
bear_room(bear_moved)
def cthulhu_room():
print "Here you see the great evil Cthulhu."
print "He, it, whatever stares at you and you go insane."
print "Do you flee for your life or eat your head?"
next = raw_input("> ").lower()
if 'flee' in next:
start()
elif 'head' in next:
dead("Well that was tasy!")
else:
cthuhlu_room()
def dead(why):
print why, "Game Over!"
exit(0)
def start():
print "You're in a dark room."
print "You have no idea who you are or how you got here."
print "Your head is throbbing..."
print "There are two doors, one to your left, and one to your right."
print "Which door do you take?"
next = raw_input("> ").lower()
if 'left' in next:
bear_room(bear_moved)
elif 'right' in next:
cthulhu_room()
else:
start()
start()
这是您的 bear_room 函数的顶部,全局功能已修复。您仍然需要适当地更改对例程的调用。
您还在学习布尔值;请注意我在您的测试中所做的更改。你不必测试 bear_moved == True;变量本身 是 一个 True/False 值。将它与 True 进行比较没有任何作用。将其与 False 进行比较只是 not 操作。我删除了你的 else: pass 因为到达那个位置的唯一方法是如果 bear_moved 是 NaN (不是数字),a您不应在此程序中看到的值。从逻辑上讲,您根本无法理解该子句。
当然,你的程序还有待改进:修复拼写和语法错误,让逻辑流程更清晰一点(你为此做了什么流程图吗?),嵌套if语句以节省工作和阅读时间。
def bear_room():
global bear_moved
if not bear_moved:
print "There's a bear in here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
else:
print "The bear has moved away from the door."
我进一步研究了 Python 的全局变量和局部变量。我现在意识到我正在更改函数内的局部变量,而不是全局变量 bear_moved。这就是为什么这只熊在重新进入时把它胖乎乎的自己扑倒在门前的原因。
从 bear_room() 函数中剥离参数并将其内部的 bear_moved 引用设为全局引用后,该函数按照我最初希望的方式工作。我还用简单的 bear_moved 或不 bear_moved.
替换了 bear_moved == (布尔)表达式
from sys import exit
bear_moved = False
# Exercise 35 of Zed Shaw's LPTHW
# Player can now revisit previous rooms
# The bear_room function uses simple recursion
# instead of the while loop of the original script
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
if next.isdigit():
if int(next) > 101:
dead("You greedy bastard!")
elif int(next) < 101:
print "Nice, you're not greedy! You win!"
exit(0)
else:
pass
elif 'back' in next or 'backtrack' in next:
bear_room()
else:
print "Type a number!"
gold_room()
def bear_room():
"""Globalizing bear_moved variable
and stripping parameter as advised."""
global bear_moved
if not bear_moved:
print "There's a bear in here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
elif bear_moved:
print "The bear has moved away from the door."
next = raw_input("> ")
if 'honey' in next:
dead("The looks at you and then slaps your face off.")
elif 'taunt' in next and not bear_moved:
bear_moved = True
bear_room()
elif 'taunt' in next and bear_moved:
dead("The bear flies into a rage and chews your leg off.")
elif 'open' in next and bear_moved:
gold_room()
elif 'open' in next and not bear_moved:
dead("The bear, still standing in the way, lunges up and rips your throat out.")
elif 'back' in next or 'backtrack' in next:
start()
else:
print "I got no idea what this means."
bear_room()
def cthulhu_room():
print "Here you see the great evil Cthulhu."
print "He, it, whatever stares at you and you go insane."
print "Do you flee for your life or eat your head?"
next = raw_input("> ").lower()
if 'flee' in next or 'run' in next:
start()
elif 'head' in next:
dead("Well that was tasy!")
else:
cthulhu_room()
def dead(why):
print why, "Game Over!"
exit(0)
def start():
print "You're in a dark room."
print "You have no idea who you are or how you got here."
print "Your head is throbbing..."
print "There are two doors, one to your left, and one to your right."
print "Which door do you take?"
next = raw_input("> ").lower()
if 'left' in next:
bear_room()
elif 'right' in next:
cthulhu_room()
else:
start()
start()
我想为 Zed Shaw 的 LPTHW 练习 35 添加更多功能。脚本 运行 没有崩溃,并允许玩家按照我的意愿重新访问以前的房间。然而,在 bear_room 中,玩家唯一可以通过的方法就是对着熊尖叫,将 bear_moved 布尔值切换为 True。
如果玩家这样做然后倒退到起始房间,我的意图是 bear_moved 布尔值保持在 True 位置,这意味着熊仍然会被移离门重新进入。
当我 运行 脚本时,这并没有发生;第一次进入 bear_room 时,我对着熊尖叫,让它离开了门。然后我退出房间,回到开始的房间。当我回到 bear_room 时,那只熊又神秘地扑通一声趴在门前。
出于这个目的,我将 bear_moved 布尔值放在函数之外——这是我唯一能想到的为程序提供额外功能的方法。
回顾一下,当我退出 bear_room 并重新进入时,为什么熊不移动?我怎样才能实现我想要的功能?
from sys import exit
bear_moved = False
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
if next.isdigit():
if int(next) > 101:
dead("You greedy bastard!")
elif int(next) < 101:
print "Nice, you're not greedy! You win!"
exit(0)
else:
pass
elif 'back' in next or 'backtrack' in next:
bear_room(bear_moved)
else:
print "Type a number!"
gold_room()
def bear_room(bear_moved):
if bear_moved == False:
print "There's a bear in here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
elif bear_moved == True:
print "The bear has moved away from the door."
else:
pass
next = raw_input("> ")
if 'honey' in next:
dead("The looks at you and then slaps your face off.")
elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == False:
bear_moved = True
bear_room(bear_moved)
elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == True:
dead("The bear flies into a rage and chews your leg off.")
elif 'open' in next and bear_moved == True:
gold_room()
elif 'open' in next and bear_moved == False:
dead("The bear, still standing in the way, lunges up and rips your throat out.")
elif 'back' in next or 'backtrack' in next:
start()
else:
print "I got no idea what this means."
bear_room(bear_moved)
def cthulhu_room():
print "Here you see the great evil Cthulhu."
print "He, it, whatever stares at you and you go insane."
print "Do you flee for your life or eat your head?"
next = raw_input("> ").lower()
if 'flee' in next:
start()
elif 'head' in next:
dead("Well that was tasy!")
else:
cthuhlu_room()
def dead(why):
print why, "Game Over!"
exit(0)
def start():
print "You're in a dark room."
print "You have no idea who you are or how you got here."
print "Your head is throbbing..."
print "There are two doors, one to your left, and one to your right."
print "Which door do you take?"
next = raw_input("> ").lower()
if 'left' in next:
bear_room(bear_moved)
elif 'right' in next:
cthulhu_room()
else:
start()
start()
这是您的 bear_room 函数的顶部,全局功能已修复。您仍然需要适当地更改对例程的调用。
您还在学习布尔值;请注意我在您的测试中所做的更改。你不必测试 bear_moved == True;变量本身 是 一个 True/False 值。将它与 True 进行比较没有任何作用。将其与 False 进行比较只是 not 操作。我删除了你的 else: pass 因为到达那个位置的唯一方法是如果 bear_moved 是 NaN (不是数字),a您不应在此程序中看到的值。从逻辑上讲,您根本无法理解该子句。
当然,你的程序还有待改进:修复拼写和语法错误,让逻辑流程更清晰一点(你为此做了什么流程图吗?),嵌套if语句以节省工作和阅读时间。
def bear_room():
global bear_moved
if not bear_moved:
print "There's a bear in here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
else:
print "The bear has moved away from the door."
我进一步研究了 Python 的全局变量和局部变量。我现在意识到我正在更改函数内的局部变量,而不是全局变量 bear_moved。这就是为什么这只熊在重新进入时把它胖乎乎的自己扑倒在门前的原因。
从 bear_room() 函数中剥离参数并将其内部的 bear_moved 引用设为全局引用后,该函数按照我最初希望的方式工作。我还用简单的 bear_moved 或不 bear_moved.
替换了 bear_moved == (布尔)表达式from sys import exit
bear_moved = False
# Exercise 35 of Zed Shaw's LPTHW
# Player can now revisit previous rooms
# The bear_room function uses simple recursion
# instead of the while loop of the original script
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
if next.isdigit():
if int(next) > 101:
dead("You greedy bastard!")
elif int(next) < 101:
print "Nice, you're not greedy! You win!"
exit(0)
else:
pass
elif 'back' in next or 'backtrack' in next:
bear_room()
else:
print "Type a number!"
gold_room()
def bear_room():
"""Globalizing bear_moved variable
and stripping parameter as advised."""
global bear_moved
if not bear_moved:
print "There's a bear in here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
elif bear_moved:
print "The bear has moved away from the door."
next = raw_input("> ")
if 'honey' in next:
dead("The looks at you and then slaps your face off.")
elif 'taunt' in next and not bear_moved:
bear_moved = True
bear_room()
elif 'taunt' in next and bear_moved:
dead("The bear flies into a rage and chews your leg off.")
elif 'open' in next and bear_moved:
gold_room()
elif 'open' in next and not bear_moved:
dead("The bear, still standing in the way, lunges up and rips your throat out.")
elif 'back' in next or 'backtrack' in next:
start()
else:
print "I got no idea what this means."
bear_room()
def cthulhu_room():
print "Here you see the great evil Cthulhu."
print "He, it, whatever stares at you and you go insane."
print "Do you flee for your life or eat your head?"
next = raw_input("> ").lower()
if 'flee' in next or 'run' in next:
start()
elif 'head' in next:
dead("Well that was tasy!")
else:
cthulhu_room()
def dead(why):
print why, "Game Over!"
exit(0)
def start():
print "You're in a dark room."
print "You have no idea who you are or how you got here."
print "Your head is throbbing..."
print "There are two doors, one to your left, and one to your right."
print "Which door do you take?"
next = raw_input("> ").lower()
if 'left' in next:
bear_room()
elif 'right' in next:
cthulhu_room()
else:
start()
start()