python - 输入中的拼写错误仍然符合在 UNIX 上使用 Python 2.7 进行基于文本的 RPG 游戏的条件
问题描述
一个月以来,我一直在使用“Learn Python The Hard Way”教程学习如何编码。到目前为止,这很有趣。我挑战自己创建基于文本的 RPG 游戏。
我目前正在重写代码以提高可读性和维护性。
我面临一个问题:
如果我输入选择输入“open heaby door”(注意拼写错误),条件仍然满足,我得到预期的打印,就好像它没有拼写错误一样。
关于我做错了什么的任何想法?
谢谢您的帮助 :)
PS:我还有另一个问题,但我想我必须在单独的问题文件中提问。
prison_key = False
def test():
global prison_key
curr_prompt = "What do you do?"
print curr_prompt
choice = raw_input("> ").lower()
while "quit" not in choice:
if "go" in choice:
if "cellar" in choice:
print "cellar"
elif "gravel" in choice or "path" in choice:
print "gravel path"
elif "prison" in choice and prison_key:
print "You enter the prison."
elif "prison" in choice:
print "The door is locked."
else:
print "Invalid"
elif "search" in choice:
if "search" == choice:
print "invalid"
elif "prison":
print "The door is closed."
elif "bucket" in choice:
print "The bucket is empty."
elif "keg" in choice:
prison_key = True
print "You find a key."
elif "door" in choice and ("heavy" in choice or "steel" in choice or "metal" in choice):
print "It looks like a prison door"
elif "search door" == choice:
print "Which one?"
else:
print "You find nothing."
elif "open door" == choice:
print "which one?"
elif "open" in choice:
if "door" in choice and ("wooden" in choice or "cellar" in choice):
print "gravel path"
elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice) and prison_key:
print "You open the prison"
elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice):
print "the door is locked"
elif "prison" in choice and prison_key:
print "You enter the prison."
elif "prison" in choice:
print "the door is locked."
else:
print "invalid"
elif "drink" in choice and "wine" in choice:
print "You alcoholic."
else:
print "invalid"
print curr_prompt
choice = raw_input("> ").lower()
exit(0)
test()
解决方案
所以发现你的问题。在 elif 条件下
elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice):
您正在再次检查门,因此它通过了 if 条件。同样适用于这种情况
elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice) and prison_key:
print "the door is locked"
推荐阅读
- php - 如何将加载的自定义计费字段保存到 woocommerce 订单元(管理员订单)?
- c++ - 错误:构建包 ncurses:x86-windows 失败:BUILD_FAILED
- redux - 在 redux 或 react-redux 中面临登录页面问题
- c# - 使用 mpxj 从 mpp 文件中读取任务企业自定义字段值
- python - Tensorflow:如何将注意力层的输出作为输入参数返回?
- flutter - 在使用谷歌登录并收到此错误时
- java - 是什么导致了这个“jpackage 不存在”错误?
- c# - WPF 将 DataGridComboBoxColumn 绑定到 ComboBox 的 SelectedItem
- html - jQuery 在添加/删除唯一类名时在 X 个按钮之间切换“活动”类
- visual-studio - (FORTRAN 90)Visual Studio Watch 不显示变量,尽管它们出现在本地调试模式下 - 标识符未定义