teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

改善 0 ~ 5 の KEY は不要でした。

2021/01/01 12:37

投稿

lehshell
lehshell

スコア1180

answer CHANGED
@@ -28,4 +28,34 @@
28
28
  sp_ans = special_quest(sp_dic)
29
29
  print(*sp_ans.items())
30
30
  # 以下共通の質問を行う
31
+ ```
32
+ #改善コード
33
+ 辞書の不要な KEY 0 ~ 5 を削除しリストに
34
+ ```Python
35
+ import random
36
+
37
+ # 専用質問
38
+ sp_lst = [
39
+ ('お題1', '質問1', '質問2', '質問3'),
40
+ ('お題2', '質問4', '質問5', '質問6'),
41
+ ('お題3', '質問7', '質問8', '質問9'),
42
+ ('お題4','質問10','質問11','質問12'),
43
+ ('お題5','質問13','質問14','質問15'),
44
+ ('お題6','質問16','質問17','質問18')
45
+ ]
46
+
47
+ def special_quest(lst):
48
+ """
49
+ lst format: [('お題1', '質問1', '質問2', ...), (...), ...]
50
+ """
51
+ ans = dict()
52
+ idx = random.randrange(len(lst)) # 0 ~ 5 のどれか: 5 は len(lst)-1
53
+ print(lst[idx][0])
54
+ for st in lst[idx][1:]:
55
+ ans[st] = input(st + " > ")
56
+ return ans
57
+
58
+ sp_ans = special_quest(sp_lst)
59
+ print(*sp_ans.items())
60
+ # 以下共通の質問を行う
31
61
  ```

1

defaultdict は不要のため修正

2021/01/01 12:37

投稿

lehshell
lehshell

スコア1180

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  こんな感じでどうでしょうか?
2
2
  ```Python
3
3
  import random
4
- from collections import defaultdict
4
+ #from collections import defaultdict
5
5
 
6
6
  # 専用質問 パターン 0 ~ 5
7
7
  sp_dic = {
@@ -17,7 +17,8 @@
17
17
  """
18
18
  dic format: {0:('お題1', '質問1', '質問2', ...), 1:(...), ...}
19
19
  """
20
- ans = defaultdict(list)
20
+ #ans = defaultdict(list)
21
+ ans = dict()
21
22
  idx = random.randrange(len(dic)) # 0 ~ 5 のどれか: 5 は len(dic)-1
22
23
  print(dic[idx][0])
23
24
  for n in range(1, len(dic[idx])):