質問編集履歴
1
ptna.pyとresponder.pyのコードを追加しました。ご指摘ありがとうございます。
test
CHANGED
File without changes
|
test
CHANGED
@@ -27,3 +27,95 @@
|
|
27
27
|
ptna = Ptna("ptna")
|
28
28
|
TypeError: Ptna() takes no arguments
|
29
29
|
```
|
30
|
+
すみません、ptna.pyとresponder.pyのコードも添付しておきます。
|
31
|
+
```ptna.py
|
32
|
+
from responder import *
|
33
|
+
|
34
|
+
class Ptna:
|
35
|
+
"""ピティナの本体クラス
|
36
|
+
"""
|
37
|
+
def _init_(self, name):
|
38
|
+
"""Ptnaオブジェクトの名前をnameに格納
|
39
|
+
RandomREsponderオブジェクトを生成してresponderに格納
|
40
|
+
|
41
|
+
@param name Ptnaオブジェクトの名前
|
42
|
+
"""
|
43
|
+
self.name = name
|
44
|
+
self.responder = RandomResponder("Random")
|
45
|
+
|
46
|
+
def dialogue(self, input):
|
47
|
+
"""応答オブジェクトのresponse()を呼び出して
|
48
|
+
応答文字列を取得する
|
49
|
+
|
50
|
+
@param input ユーザーによって入力された文字列
|
51
|
+
戻り値 応答文字列
|
52
|
+
"""
|
53
|
+
return self.responder.response(input)
|
54
|
+
|
55
|
+
def get_responder_name(self):
|
56
|
+
"""応答オブジェクトの名前を返す
|
57
|
+
"""
|
58
|
+
return self.responder.name
|
59
|
+
|
60
|
+
def get_name(self):
|
61
|
+
"""Ptnaオブジェクトの名前を返す
|
62
|
+
"""
|
63
|
+
return self.name
|
64
|
+
```
|
65
|
+
```responder.py
|
66
|
+
import random
|
67
|
+
|
68
|
+
class Responder:
|
69
|
+
"""応答クラスのスーパークラス
|
70
|
+
"""
|
71
|
+
def _init_(self, name):
|
72
|
+
"""Responderオブザーバーの名前をnameに格納
|
73
|
+
|
74
|
+
@param name Responderオブジェクトの名前
|
75
|
+
"""
|
76
|
+
self.name = name
|
77
|
+
|
78
|
+
def response(self, input):
|
79
|
+
"""オーバーライドを前提としたresponse()メソッド
|
80
|
+
|
81
|
+
@param input 入力された文字列
|
82
|
+
戻り値 空の文字列
|
83
|
+
"""
|
84
|
+
return ""
|
85
|
+
|
86
|
+
def get_name(self):
|
87
|
+
"""応答オブジェクトの名前を返す
|
88
|
+
"""
|
89
|
+
return self.name
|
90
|
+
|
91
|
+
class RepeatResponder(Responder):
|
92
|
+
"""オウム返しのための行うサブクラス
|
93
|
+
"""
|
94
|
+
def response(self,input):
|
95
|
+
"""応答文字列を作って返す
|
96
|
+
|
97
|
+
@param input 入力された文字列
|
98
|
+
"""
|
99
|
+
return "{}ってなに?".format(input)
|
100
|
+
|
101
|
+
class RandomResponder(Responder):
|
102
|
+
"""ランダムな応答のための行うサブクラス
|
103
|
+
"""
|
104
|
+
def _init_(self, name):
|
105
|
+
"""Responderオブジェクトの名前を引数にして
|
106
|
+
スーパークラスの_init_()を呼び出す
|
107
|
+
ランダムに抽出するメッセージを格納したリストを作成
|
108
|
+
|
109
|
+
@param name Responderオブジェクトの名前
|
110
|
+
"""
|
111
|
+
super()._init(name)
|
112
|
+
self.response = ["いい天気だね", "君はパーリーピーポー", "10円拾った"]
|
113
|
+
|
114
|
+
def response(self, input):
|
115
|
+
"""応答文字列を作って返す
|
116
|
+
|
117
|
+
@param input 入力された文字列
|
118
|
+
戻り値 リストからランダムに抽出した文字列
|
119
|
+
"""
|
120
|
+
return (random.choice(self.responses))
|
121
|
+
```
|