尝试使用循环搜索消息

时间:2018-01-26 16:06:04

标签: python-2.7

我正在尝试编写将搜索消息的代码

  快速的棕色狐狸跳过懒狗

并将继续接收用户的输入,直到用户输入exit。我还必须将上面定义为可以重复调用的函数。任何帮助,将不胜感激。这就是我所拥有的。

message = 'the quick brown fox jumped over the lazy dog'

def search(keyWord):
    if raw_input('Please enter a word from the hidden phrase ') in message:
        print 'That word IS part of the message '
    else:
        print 'That word is NOT part of the message'


while True:
print 'Please try to guess the hidden phrase '
search('run')


print 'Thanks for guessing '

1 个答案:

答案 0 :(得分:0)

试试这个。

message = 'the quick brown fox jumped over the lazy dog'

def search(keyWord):
    if keyWord in message:
        print "That word IS part of the message. "
    else:
        print "That word is NOT part of the message"

print 'Please try to guess the hidden phrase '
while True:
    keyWord = raw_input("Please enter a word from the hidden phrase, or 'exit' to quit:")
    if keyWord == 'exit':
        break
    search(keyWord)    

print 'Thanks for guessing '

<强>结果

>>> 
Please try to guess the hidden phrase 
Please enter a word from the hidden phrase, or 'exit' to quit:the
That word IS part of the message. 
Please enter a word from the hidden phrase, or 'exit' to quit:quick
That word IS part of the message. 
Please enter a word from the hidden phrase, or 'exit' to quit:quit
That word is NOT part of the message
Please enter a word from the hidden phrase, or 'exit' to quit:exit
Thanks for guessing 
>>>