Python 3 - 如果语句不匹配完全字符串

时间:2016-06-20 19:44:31

标签: python python-3.x

我遵循"学习Python艰难的方式"书。在下面的部分示例中,将输入字符串与某些值进行比较。

当我执行代码时,如果您输入任何包含单词+任何其他字符的输入,它仍会被评估为True,

  

e.g。 > fleeee,headsss,headhead

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?"

 choice = raw_input("> ")

 if "flee" in choice:
    start()
 elif "head" in choice:
    dead("Well that was tasty!")
 else:
    cthulhu_room()

如何修改它以使其匹配' head'到底是什么?

1 个答案:

答案 0 :(得分:4)

使用==代替in

使用in搜索是否在里面 使用==检查确切的字符串。

相关问题