【发布时间】:2016-03-31 09:12:03
【问题描述】:
我是编程新手,我正在通过《Learning Python the Hard Way》一书学习 Python。我在练习 36,要求我们编写自己的简单游戏。
http://learnpythonthehardway.org/book/ex36.html
(问题是,当我在走廊(或更准确地说,在“选择”中)并且我写“门”时,游戏的反应就好像我说的是“老人”等“男人”。
我做错了什么?)
编辑:我不应该写 'if "oldman" or "man" in choice' 而是在每个选项后加上 'inchoice'。
然而,下一个问题.. 这个人从不站起来骂我,它一直皱着眉头。为什么游戏没有进行到那个elif?
experiences = []
def choices():
while True:
choice = raw_input("What do you wish to do? ").lower()
if "oldman" in choice or "man" in choice or "ask" in choice:
print oldman
elif "gate" in choice or "fight" in choice or "try" in choice and not "pissedoff" in experiences:
print "The old man frowns. \"I said, you shall not pass through this gate until you prove yourself worthy!\""
experiences.append("pissedoff")
elif "gate" in choice or "fight" in choice or "try" in choice and "pissedoff" in experiences and not "reallypissed" in experiences:
print "The old man is fed up. \"I told you not to pass this gate untill you are worthy! Try me again and you will die.\""
experiences.append("reallypissed")
elif "gate" in choice or "fight" in choice or "try" in choice and "reallypissed" in experiences:
print "The old man stands up and curses you. You die"
exit()
elif "small" in choice:
print "You go into the small room"
firstroom()
else:
print "Come again?"
编辑:已修复!!
def choices():
while True:
choice = raw_input("What do you wish to do? ").lower()
if choice in ['oldman', 'man', 'ask']:
print oldman
elif choice in ['gate', 'fight', 'try'] and not 'pissedoff' in experiences:
print "The old man frowns. \"I said, you shall not pass through this gate until you prove yourself worthy!\""
experiences.append("pissedoff")
elif choice in ['gate', 'fight', 'try'] and not 'reallypissed' in experiences:
print "The old man is fed up. \"I told you not to pass this gate untill you are worthy! Try me again and you will die.\""
experiences.append("reallypissed")
elif choice in ['gate', 'fight', 'try'] and 'reallypissed' in experiences:
print "The old man stands up and curses you. You die"
exit()
elif "small" in choice:
print "You go into the small room"
firstroom()
else:
print "Come again?"
感谢您的所有帮助:)。
【问题讨论】:
-
我写了三遍“门”,我要让老人站起来骂人。在第一次将“生气”添加到体验列表后,如果我再次输入门它应该警告我并将真的很生气添加到体验列表中,如果我第三次输入“门”它应该退出。
-
嘿,我有一个建议:您可以使用
if choice in [..., ...]重写 if/elif 以使其更容易理解吗?我一会儿回来看看! -
我做到了,我想我修好了!非常感谢您的帮助:)。
标签: python