1

私は現在python-jabberbotをいじっていて、ランダムな文を送信する簡単なメソッドを作成するのに問題があります。私はPythonに堪能ではないので、どこが間違っているのか疑問に思っています。配列を宣言する方法が私の没落であると感じています:

def whatdoyouknow(self, mess, args):
        """random response"""   
        string[0] = 'this is a longish sentence about things'
        string[1] = 'this is a longish sentence about things number 2'
        string[2] = 'this is a longish sentence about things number 3'

        i = random.randint(0, 2)
        return string[i]
4

1 に答える 1

7

要素を角括弧で囲んでリスト リテラルを定義します。

string = ['this is a longish sentence about things',
          'this is a longish sentence about things number 2',
          'this is a longish sentence about things number 3']

または、空のリストを定義してリストを作成し、要素を追加します。

string = []
string.append('this is a longish sentence about things')
string.append('this is a longish sentence about things number 2')
string.append('this is a longish sentence about things number 3')

続行する前に、 Python チュートリアルを読むことを強くお勧めします。Python 型の構築とそれらの操作方法が説明されています。

于 2013-01-28T13:28:40.770 に答える