我在python中的代码不起作用

时间:2013-01-19 17:28:22

标签: python

  

可能重复:
  OR behaviour in python:

我是编程的初学者,并选择python作为我的第一语言..

print "start the conversation"
conversation = raw_input()

if conversation == "Hi" or "hi" or "Hello" or "hello":
    print "Hey there!"

elif conversation == "How are you?" or "how are you?":
    print "I'm good and you?"
else:
    print "No one starts a conversation like this."

但是当我运行程序时它工作正常我输入“嗨”它回复“嘿那里!”但每当我输入以下内容作为输入“你好吗?”它仍打印出“嘿那里!”我希望它打印出来“我很好,你呢?”而不是“嘿那里!”再次。因为我是初学者,请放轻松。

2 个答案:

答案 0 :(得分:1)

if conversation == "Hi" or "hi" or "Hello" or "hello":

应该阅读

if conversation in ("Hi", "hi", "Hello", "hello"):

同样适用于elif

您现在拥有的代码在语法上是有效的,但不会按照您的想法执行(它基本上总是计算为True)。

答案 1 :(得分:1)

你的第一个条件总是如实。 你应该使用:

if conversation == "Hi" or conversation == "hi" or conversation == "Hello" or conversation == "hello":

if conversation in ("Hi", "hi", "Hello", "hello"):
相关问题