回答編集履歴

5

不正な西暦を入力したときの挙動を変更

2018/03/09 09:06

投稿

LouiS0616
LouiS0616

スコア35658

test CHANGED
@@ -170,13 +170,15 @@
170
170
 
171
171
 
172
172
 
173
+ year_str = year_strs[0]
174
+
173
175
  try:
174
176
 
175
- year = int(year_strs[0])
177
+ year = int(year_str)
176
178
 
177
179
  except ValueError:
178
180
 
179
- return f'{year_strs}は西暦だと解釈できないよ。'
181
+ return f'{year_str}は西暦だと解釈できないよ。'
180
182
 
181
183
 
182
184
 
@@ -210,7 +212,7 @@
210
212
 
211
213
 
212
214
 
213
- **実行例** おまけ:[Wandbox](https://wandbox.org/permlink/U3TYkSzRQUou8yJj)
215
+ **実行例** おまけ:[Wandbox](https://wandbox.org/permlink/m8LEaEpbGsMce0Eb)
214
216
 
215
217
  ```plain
216
218
 
@@ -232,6 +234,12 @@
232
234
 
233
235
 
234
236
 
237
+ なんか入力してね: 干支 3.14
238
+
239
+ 3.14は西暦だと解釈できないよ。
240
+
241
+
242
+
235
243
  なんか入力してね: 選ぶ
236
244
 
237
245
  そもそも選択肢がないよ。
@@ -240,13 +248,25 @@
240
248
 
241
249
  なんか入力してね: 選ぶ 1 2 3
242
250
 
251
+ 「1」が選ばれました。
252
+
253
+
254
+
255
+ なんか入力してね: 選ぶ 1 2 3
256
+
257
+ 「1」が選ばれました。
258
+
259
+
260
+
261
+ なんか入力してね: 選ぶ 1 2 3
262
+
243
263
  「2」が選ばれました。
244
264
 
245
265
 
246
266
 
247
- なんか入力してね: 選ぶ 1 2 3
267
+ なんか入力してね: 面白いこと言って
248
-
268
+
249
- 3」が選ばれした
269
+ 面白いこと言っての意味わかりせん
250
270
 
251
271
 
252
272
 

4

改行からうちでもエラーが出ないようにした

2018/03/09 09:06

投稿

LouiS0616
LouiS0616

スコア35658

test CHANGED
@@ -120,6 +120,10 @@
120
120
 
121
121
  input_str = input('なんか入力してね: ')
122
122
 
123
+ if not input_str:
124
+
125
+ continue
126
+
123
127
 
124
128
 
125
129
  if 'さようなら' == input_str:
@@ -206,7 +210,7 @@
206
210
 
207
211
 
208
212
 
209
- **実行例** おまけ:[Wandbox](https://wandbox.org/permlink/DNzr1cohieqZXeBe)
213
+ **実行例** おまけ:[Wandbox](https://wandbox.org/permlink/U3TYkSzRQUou8yJj)
210
214
 
211
215
  ```plain
212
216
 
@@ -246,11 +250,7 @@
246
250
 
247
251
 
248
252
 
249
- なんか入力してね: 面白いこと言って
253
+ なんか入力してね:
250
-
251
- 「面白いこと言って」の意味がわかりません。
252
-
253
-
254
254
 
255
255
  なんか入力してね: さようなら
256
256
 

3

コードの修正

2018/03/09 09:01

投稿

LouiS0616
LouiS0616

スコア35658

test CHANGED
@@ -158,21 +158,21 @@
158
158
 
159
159
 
160
160
 
161
- def eto_command(year_str=None):
161
+ def eto_command(*year_strs):
162
-
162
+
163
- if year_str is None:
163
+ if len(year_strs) != 1:
164
-
164
+
165
- return 'コマンドと一緒に西暦入力してね。'
165
+ return 'コマンドと一緒に西暦を一つだけ入力してね。'
166
166
 
167
167
 
168
168
 
169
169
  try:
170
170
 
171
- year = int(year_str)
171
+ year = int(year_strs[0])
172
172
 
173
173
  except ValueError:
174
174
 
175
- return f'{year_str}は西暦だと解釈できないよ。'
175
+ return f'{year_strs}は西暦だと解釈できないよ。'
176
176
 
177
177
 
178
178
 
@@ -206,13 +206,13 @@
206
206
 
207
207
 
208
208
 
209
- **実行例** おまけ:[Wandbox](https://wandbox.org/permlink/4rd3yuofaGzaTGqd)
209
+ **実行例** おまけ:[Wandbox](https://wandbox.org/permlink/DNzr1cohieqZXeBe)
210
210
 
211
211
  ```plain
212
212
 
213
213
  なんか入力してね: 干支
214
214
 
215
- コマンドと一緒に西暦入力してね。
215
+ コマンドと一緒に西暦を一つだけ入力してね。
216
216
 
217
217
 
218
218
 
@@ -222,9 +222,9 @@
222
222
 
223
223
 
224
224
 
225
- なんか入力してね: 干支 3.14
225
+ なんか入力してね: 干支 2018 2019
226
-
226
+
227
- 3.14は西暦だと解釈できないよ
227
+ コマンドと一緒に西暦を一つけ入力してね
228
228
 
229
229
 
230
230
 
@@ -242,13 +242,7 @@
242
242
 
243
243
  なんか入力してね: 選ぶ 1 2 3
244
244
 
245
- 2」が選ばれました。
245
+ 3」が選ばれました。
246
-
247
-
248
-
249
- なんか入力してね: 選ぶ 1 2 3
250
-
251
- 「1」が選ばれました。
252
246
 
253
247
 
254
248
 

2

追記

2018/03/09 08:55

投稿

LouiS0616
LouiS0616

スコア35658

test CHANGED
@@ -67,3 +67,199 @@
67
67
  0. コードをハイライトした状態で<code>ボタンを押す
68
68
 
69
69
  0. マークダウンを手動で入力する [Qiita - Markdown記法 チートシート - コードの挿入](https://qiita.com/Qiita/items/c686397e4a0f4f11683d#code---%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AE%E6%8C%BF%E5%85%A5)
70
+
71
+
72
+
73
+ 書いてみた
74
+
75
+ ---
76
+
77
+ 部分的ですが、参考までに。
78
+
79
+
80
+
81
+ **main.py**
82
+
83
+ ```Python
84
+
85
+ from command import eto_command, choice_command
86
+
87
+
88
+
89
+ command_dict = {
90
+
91
+ '干支': eto_command,
92
+
93
+ '選ぶ': choice_command,
94
+
95
+ }
96
+
97
+
98
+
99
+ def interpret_command(input_str):
100
+
101
+ command, *args = input_str.split()
102
+
103
+
104
+
105
+ if command in command_dict:
106
+
107
+ return command_dict[command](*args)
108
+
109
+
110
+
111
+ return f'「{command}」の意味がわかりません。'
112
+
113
+
114
+
115
+
116
+
117
+ def main():
118
+
119
+ while True:
120
+
121
+ input_str = input('なんか入力してね: ')
122
+
123
+
124
+
125
+ if 'さようなら' == input_str:
126
+
127
+ print('了解。システムを終了します。')
128
+
129
+ break
130
+
131
+
132
+
133
+ print(
134
+
135
+ interpret_command(input_str), end='\n\n'
136
+
137
+ )
138
+
139
+
140
+
141
+
142
+
143
+ if __name__ == '__main__':
144
+
145
+ main()
146
+
147
+ ```
148
+
149
+
150
+
151
+ **command.py**
152
+
153
+ ```Python
154
+
155
+ import random
156
+
157
+
158
+
159
+
160
+
161
+ def eto_command(year_str=None):
162
+
163
+ if year_str is None:
164
+
165
+ return 'コマンドと一緒に西暦も入力してね。'
166
+
167
+
168
+
169
+ try:
170
+
171
+ year = int(year_str)
172
+
173
+ except ValueError:
174
+
175
+ return f'{year_str}は西暦だと解釈できないよ。'
176
+
177
+
178
+
179
+ eto = [
180
+
181
+ "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"
182
+
183
+ ][(year + 8) % 12]
184
+
185
+
186
+
187
+ return f'{year}年生まれの干支は「{eto}」です。'
188
+
189
+
190
+
191
+
192
+
193
+ def choice_command(*candidate_list):
194
+
195
+ if not candidate_list:
196
+
197
+ return 'そもそも選択肢がないよ。'
198
+
199
+
200
+
201
+ choiced = random.choice(candidate_list)
202
+
203
+ return f'「{choiced}」が選ばれました。'
204
+
205
+ ```
206
+
207
+
208
+
209
+ **実行例** おまけ:[Wandbox](https://wandbox.org/permlink/4rd3yuofaGzaTGqd)
210
+
211
+ ```plain
212
+
213
+ なんか入力してね: 干支
214
+
215
+ コマンドと一緒に西暦も入力してね。
216
+
217
+
218
+
219
+ なんか入力してね: 干支 2018
220
+
221
+ 2018年生まれの干支は「戌」です。
222
+
223
+
224
+
225
+ なんか入力してね: 干支 3.14
226
+
227
+ 3.14は西暦だと解釈できないよ。
228
+
229
+
230
+
231
+ なんか入力してね: 選ぶ
232
+
233
+ そもそも選択肢がないよ。
234
+
235
+
236
+
237
+ なんか入力してね: 選ぶ 1 2 3
238
+
239
+ 「2」が選ばれました。
240
+
241
+
242
+
243
+ なんか入力してね: 選ぶ 1 2 3
244
+
245
+ 「2」が選ばれました。
246
+
247
+
248
+
249
+ なんか入力してね: 選ぶ 1 2 3
250
+
251
+ 「1」が選ばれました。
252
+
253
+
254
+
255
+ なんか入力してね: 面白いこと言って
256
+
257
+ 「面白いこと言って」の意味がわかりません。
258
+
259
+
260
+
261
+ なんか入力してね: さようなら
262
+
263
+ 了解。システムを終了します。
264
+
265
+ ```

1

追記

2018/03/09 08:50

投稿

LouiS0616
LouiS0616

スコア35658

test CHANGED
@@ -37,3 +37,33 @@
37
37
 
38
38
 
39
39
  Wandboxにコードを起こして再現させたので、一応共有しておきます。[Wandbox](https://wandbox.org/permlink/v0rKWG2DotBLV2un)
40
+
41
+
42
+
43
+ コードブロックの適用の仕方
44
+
45
+ ---
46
+
47
+ 次のようにコードブロックを利用出来ます。
48
+
49
+ ```Python
50
+
51
+ def main():
52
+
53
+ print('Hello World!')
54
+
55
+
56
+
57
+ if __name__ == __main__:
58
+
59
+ main()
60
+
61
+ ```
62
+
63
+
64
+
65
+ 方法は二つあります。
66
+
67
+ 0. コードをハイライトした状態で<code>ボタンを押す
68
+
69
+ 0. マークダウンを手動で入力する [Qiita - Markdown記法 チートシート - コードの挿入](https://qiita.com/Qiita/items/c686397e4a0f4f11683d#code---%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AE%E6%8C%BF%E5%85%A5)