python - 检查字符串中的单个单词

标签 python user-input

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

原始代码:

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

我已经尝试过这个,但如果我输入 morningmorningwake 它会打印 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/

相关文章:

python - 如何设置 Atom 的 'styles.less' 文件以突出显示 Python 中的函数和方法调用?

python - statsmodel 预测开始和结束索引

python - 使用 ET 删除 Python 中的元素

php - 使 Web 表单输入在各种情况下都安全的正确方法是什么?

ruby - Ruby用用户输入循环

javascript - 将表中的值拆分为不同的文本输入?

python - 获取具有其他两个绝对差异的数据框

python - 如何使用 gtk3 在 python 中创建类似列表的 gui?

java - 无法在 switch 语句中接收用户输入

php - 多少限制太多?