我正在创建一个程序,我想要它做的是检查用户输入并将其与字典进行比较,看看该单词是否在字典中。
原始代码:
dic = "goodmorning" + "wakeup"
test = raw_input("test: ")
if test == dic:
print "hello"
else:
print "testf"
我已经尝试过这个,但如果我输入 morning
或 morningwake
它会打印 hello。
我也尝试过这个:
dic = ["goodmorning", "wakeup"]
test = raw_input("test: ")
if test == dic:
print "hello"
else:
print "testf"
这也不起作用。
最佳答案
使用in
,而不是==
。
dic = ["goodmorning", "wakeup"]
test = raw_input("test: ")
if test in dic:
print "hello"
else:
print "testf"
关于python - 检查字符串中的单个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17140567/