0

可能重复:
或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."

但是当我运行程序时它运行良好我输入“嗨”它回复“嘿那里!” 但每当我输入以下内容作为输入“你好吗?” 它仍然打印出“嘿!” 我想让它打印出“我很好,你呢?” 而不是“嘿!” 再次。请让它容易,因为我是一个初学者。

4

2 回答 2

1

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

应该读

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

对于elif.

您现在拥有的代码在语法上是有效的,但并没有按照您的想法执行(它基本上总是评估为True)。

于 2013-01-19T17:30:24.600 回答
1

您的第一个条件始终为 True。你应该使用:

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

或者

if conversation in ("Hi", "hi", "Hello", "hello"):
于 2013-01-19T17:32:37.010 回答