1

我正在创建一个程序,我想要它做的是检查用户输入并将其与字典进行比较,以查看该单词是否在字典中。

原始代码:

dic = "goodmorning" + "wakeup"
test = raw_input("test: ")
if test == dic:
    print "hello"
else:
    print "testf"

我已经尝试过了,但如果我要进入morning,否则morningwake它会打印你好。我也试过这个:

dic = ["goodmorning", "wakeup"]
test = raw_input("test: ")
if test == dic:
    print "hello"
else:
    print "testf"

这也行不通。

4

1 回答 1

3

使用in,不使用==

dic = ["goodmorning", "wakeup"]
test = raw_input("test: ")
if test in dic:
    print "hello"
else:
    print "testf"
于 2013-06-17T04:47:53.950 回答