2017-02-02 6 views
-1

ランダムなシノニムをリストから描画するコードを記述しようとしています。 その代わりに、自分のコードと関係のないランダムな文字列を取得しています。ここでランダムな文字列を出力として

は、メインモジュールのコードです:

from output import * 
import definitions 
from responses import * 

… 

def respond(wordList): 
    output = "" 
    for word in wordList: 
     output = (output + " " + (random.choice(word))) 
    return output 

def edison(): 
    mood = ask("Hi, " + username + "! How are you today? ") 
    if mood.lower() in definitions.positive: 
     print(respond(['i_am', 'happy', 'to' 'hear', 'that']) + "!") 
    elif mood.lower() in definitions.negative: 
     print(respond(['i_am', 'sorry_unhappy', 'to' 'hear', 'that']) + "!") 

… 

edison() 

はここresponses.pyのためのコードです:

i_am = ["I am", "I'm"] 
happy = ["cheerful", "delighted", "glad", "joyful", "joyous", "overjoyed", "pleased", "thrilled", "gleeful", "happy"] 
sorry_unhappy = ["sorry"] 
to = ["to"] 
hear = ["listen to", "hear"] 
that = ["that"] 

は、ここに私の出力のサンプルです:

Hi, Test User! How are you today? bad 
m _ h h! 
+0

:しかし、固定strの応答

はとedisonを交換してください'' i_am '' - 変数ではなく '' respond'メソッドに渡します。 'i_am'。不要なアポストロフィを削除します。 – asongtoruin

答えて

0
あなたが使用していなかった

`responses.py」からの応答に建てられました例えば ​​- あなたは、文字列を供給している

def edison(): 
    mood = ask("Hi, " + username + "! How are you today? ") 
    if mood.lower() in definitions.positive: 
     print(respond([i_am, happy, to, hear, that]) + "!") 
    elif mood.lower() in definitions.negative: 
     print(respond([i_am, sorry_unhappy, to, hear, that]) + "!") 
3

問題の可能性がありますrandom.choice(word)。単語はwordListの要素であり、文字列からのランダムな選択はランダムな文字を選択します。代わりにrandom.choice(wordList)を試してください。あなたはそれがすでに文のように何とか見えることから、出力としてワードリスト連結する場合は

、あなたはこれを行うことができます:

output = " ".join(wordList) 
関連する問題