2014-11-21 57 views
-1

當用戶在一個特定的詞,我想打印出一組的話,有點像一個搜索引擎。 E,G我在餅乾類型並將其打印出餅乾好吃 如果我鍵入wthwergebqeg打印出的字不返回任何結果 我是新來的這種蟒蛇,我已經等各種小程序和想要進入這個。如何在用戶鍵入特定單詞時打印文本?

+0

表明您已經嘗試了什麼。這裏的人希望看到一些代碼。 – 2014-11-21 20:00:34

回答

0

你可以做一本字典對這個

replies = {'cookies' : 'cookies are tasty', 
      'car': 'cars are fast', 
      'beer': 'beer is good'} 

然後,你可以使用詞典如下

>>> query = input('Enter a word : ') 
Enter a word : beer 

>>> replies.get(query) 
'beer is good' 

如果試圖.get()一個字,是不是在字典中,None會回。

-1

你可以讓詞典和檢查,如果有一個詞出現。檢查:

dictionary = {'cookies' : 'cookies are tasty', 
     'word': 'word is made of letters', 
     'cheetah': 'cheetah is fast', 
     'uncle': 'my uncle is ben', 
     'example': 'this is an example'} 

input = raw_input("Enter word:") 
if dictionary.get(input) != None: 
    print replies.get(input) 
else: 
    print "That word did not return any results" 

您可以使用列表太多,但會製造更大的代碼,因爲你需要更多的if/else語句...下面是一個例子:

list = [cookies, word, cheetah] 
input = raw_input("Enter word:") 
if input in list: 
    if input == "cookies": 
     print "Cookies are tasty" 
    if input == "word": 
     print "Word is made of letters" 
    if input == "cheetah": 
     print "Cheetah is fast" 
else: 
    print "That word did not return any results" 

我希望這helpes你解決你的問題。

相關問題