回答編集履歴
1
追加
test
CHANGED
@@ -3,3 +3,87 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
です。
|
6
|
+
|
7
|
+
これは、commentsElementsの中のリストの長さが3でないとうまく動作しません。
|
8
|
+
|
9
|
+
それぞれのリストの長さが違っていても動くようにするには以下にすると良いです。
|
10
|
+
|
11
|
+
random.choiceを使えば、それぞれの長さを気にする必要がなくなります。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
```python
|
16
|
+
|
17
|
+
import random
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
def randomcomment(cEs):
|
22
|
+
|
23
|
+
comments = "".join([random.choice(e) for e in cEs])
|
24
|
+
|
25
|
+
print(comments)
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
commentsElements = [["昨日は","今日は","明日は"],["晴れ","雨","曇り"],["!","です","ですよ"]]
|
30
|
+
|
31
|
+
randomcomment(commentsElements)
|
32
|
+
|
33
|
+
randomcomment(commentsElements)
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
commentsElements2 = [["昨日","今日","明日","明後日"],["は","も"],["晴れ","雨","曇り"],["!","です","ですよ","だね"]]
|
38
|
+
|
39
|
+
randomcomment(commentsElements2)
|
40
|
+
|
41
|
+
randomcomment(commentsElements2)
|
42
|
+
|
43
|
+
randomcomment(commentsElements2)
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
実行すると以下のようになります。
|
48
|
+
|
49
|
+
```python
|
50
|
+
|
51
|
+
>>> import random
|
52
|
+
|
53
|
+
>>>
|
54
|
+
|
55
|
+
>>> def randomcomment(cEs):
|
56
|
+
|
57
|
+
... comments = "".join([random.choice(e) for e in cEs])
|
58
|
+
|
59
|
+
... print(comments)
|
60
|
+
|
61
|
+
...
|
62
|
+
|
63
|
+
>>> commentsElements = [["昨日は","今日は","明日は"],["晴れ","雨","曇り"],["!","です","ですよ"]]
|
64
|
+
|
65
|
+
>>> randomcomment(commentsElements)
|
66
|
+
|
67
|
+
昨日は晴れ!
|
68
|
+
|
69
|
+
>>> randomcomment(commentsElements)
|
70
|
+
|
71
|
+
昨日は曇りですよ
|
72
|
+
|
73
|
+
>>>
|
74
|
+
|
75
|
+
>>> commentsElements2 = [["昨日","今日","明日","明後日"],["は","も"],["晴れ","雨","曇り"],["!","です","ですよ","だね"]]
|
76
|
+
|
77
|
+
>>> randomcomment(commentsElements2)
|
78
|
+
|
79
|
+
今日は雨!
|
80
|
+
|
81
|
+
>>> randomcomment(commentsElements2)
|
82
|
+
|
83
|
+
明日も曇りですよ
|
84
|
+
|
85
|
+
>>> randomcomment(commentsElements2)
|
86
|
+
|
87
|
+
昨日は曇りです
|
88
|
+
|
89
|
+
```
|