回答編集履歴
5
不正な西暦を入力したときの挙動を変更
answer
CHANGED
@@ -84,10 +84,11 @@
|
|
84
84
|
if len(year_strs) != 1:
|
85
85
|
return 'コマンドと一緒に西暦を一つだけ入力してね。'
|
86
86
|
|
87
|
+
year_str = year_strs[0]
|
87
88
|
try:
|
88
|
-
year = int(
|
89
|
+
year = int(year_str)
|
89
90
|
except ValueError:
|
90
|
-
return f'{
|
91
|
+
return f'{year_str}は西暦だと解釈できないよ。'
|
91
92
|
|
92
93
|
eto = [
|
93
94
|
"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"
|
@@ -104,7 +105,7 @@
|
|
104
105
|
return f'「{choiced}」が選ばれました。'
|
105
106
|
```
|
106
107
|
|
107
|
-
**実行例** おまけ:[Wandbox](https://wandbox.org/permlink/
|
108
|
+
**実行例** おまけ:[Wandbox](https://wandbox.org/permlink/m8LEaEpbGsMce0Eb)
|
108
109
|
```plain
|
109
110
|
なんか入力してね: 干支
|
110
111
|
コマンドと一緒に西暦を一つだけ入力してね。
|
@@ -115,15 +116,24 @@
|
|
115
116
|
なんか入力してね: 干支 2018 2019
|
116
117
|
コマンドと一緒に西暦を一つだけ入力してね。
|
117
118
|
|
119
|
+
なんか入力してね: 干支 3.14
|
120
|
+
3.14は西暦だと解釈できないよ。
|
121
|
+
|
118
122
|
なんか入力してね: 選ぶ
|
119
123
|
そもそも選択肢がないよ。
|
120
124
|
|
121
125
|
なんか入力してね: 選ぶ 1 2 3
|
122
|
-
「
|
126
|
+
「1」が選ばれました。
|
123
127
|
|
124
128
|
なんか入力してね: 選ぶ 1 2 3
|
125
|
-
「
|
129
|
+
「1」が選ばれました。
|
126
130
|
|
131
|
+
なんか入力してね: 選ぶ 1 2 3
|
132
|
+
「2」が選ばれました。
|
133
|
+
|
134
|
+
なんか入力してね: 面白いこと言って
|
135
|
+
「面白いこと言って」の意味がわかりません。
|
136
|
+
|
127
137
|
なんか入力してね:
|
128
138
|
なんか入力してね: さようなら
|
129
139
|
了解。システムを終了します。
|
4
改行からうちでもエラーが出ないようにした
answer
CHANGED
@@ -59,6 +59,8 @@
|
|
59
59
|
def main():
|
60
60
|
while True:
|
61
61
|
input_str = input('なんか入力してね: ')
|
62
|
+
if not input_str:
|
63
|
+
continue
|
62
64
|
|
63
65
|
if 'さようなら' == input_str:
|
64
66
|
print('了解。システムを終了します。')
|
@@ -102,7 +104,7 @@
|
|
102
104
|
return f'「{choiced}」が選ばれました。'
|
103
105
|
```
|
104
106
|
|
105
|
-
**実行例** おまけ:[Wandbox](https://wandbox.org/permlink/
|
107
|
+
**実行例** おまけ:[Wandbox](https://wandbox.org/permlink/U3TYkSzRQUou8yJj)
|
106
108
|
```plain
|
107
109
|
なんか入力してね: 干支
|
108
110
|
コマンドと一緒に西暦を一つだけ入力してね。
|
@@ -122,9 +124,7 @@
|
|
122
124
|
なんか入力してね: 選ぶ 1 2 3
|
123
125
|
「3」が選ばれました。
|
124
126
|
|
125
|
-
なんか入力してね:
|
127
|
+
なんか入力してね:
|
126
|
-
「面白いこと言って」の意味がわかりません。
|
127
|
-
|
128
128
|
なんか入力してね: さようなら
|
129
129
|
了解。システムを終了します。
|
130
130
|
```
|
3
コードの修正
answer
CHANGED
@@ -78,14 +78,14 @@
|
|
78
78
|
import random
|
79
79
|
|
80
80
|
|
81
|
-
def eto_command(
|
81
|
+
def eto_command(*year_strs):
|
82
|
-
if
|
82
|
+
if len(year_strs) != 1:
|
83
|
-
return 'コマンドと一緒に西暦
|
83
|
+
return 'コマンドと一緒に西暦を一つだけ入力してね。'
|
84
84
|
|
85
85
|
try:
|
86
|
-
year = int(
|
86
|
+
year = int(year_strs[0])
|
87
87
|
except ValueError:
|
88
|
-
return f'{
|
88
|
+
return f'{year_strs}は西暦だと解釈できないよ。'
|
89
89
|
|
90
90
|
eto = [
|
91
91
|
"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"
|
@@ -102,16 +102,16 @@
|
|
102
102
|
return f'「{choiced}」が選ばれました。'
|
103
103
|
```
|
104
104
|
|
105
|
-
**実行例** おまけ:[Wandbox](https://wandbox.org/permlink/
|
105
|
+
**実行例** おまけ:[Wandbox](https://wandbox.org/permlink/DNzr1cohieqZXeBe)
|
106
106
|
```plain
|
107
107
|
なんか入力してね: 干支
|
108
|
-
コマンドと一緒に西暦
|
108
|
+
コマンドと一緒に西暦を一つだけ入力してね。
|
109
109
|
|
110
110
|
なんか入力してね: 干支 2018
|
111
111
|
2018年生まれの干支は「戌」です。
|
112
112
|
|
113
|
-
なんか入力してね: 干支
|
113
|
+
なんか入力してね: 干支 2018 2019
|
114
|
-
|
114
|
+
コマンドと一緒に西暦を一つだけ入力してね。
|
115
115
|
|
116
116
|
なんか入力してね: 選ぶ
|
117
117
|
そもそも選択肢がないよ。
|
@@ -120,11 +120,8 @@
|
|
120
120
|
「2」が選ばれました。
|
121
121
|
|
122
122
|
なんか入力してね: 選ぶ 1 2 3
|
123
|
-
「
|
123
|
+
「3」が選ばれました。
|
124
124
|
|
125
|
-
なんか入力してね: 選ぶ 1 2 3
|
126
|
-
「1」が選ばれました。
|
127
|
-
|
128
125
|
なんか入力してね: 面白いこと言って
|
129
126
|
「面白いこと言って」の意味がわかりません。
|
130
127
|
|
2
追記
answer
CHANGED
@@ -32,4 +32,102 @@
|
|
32
32
|
|
33
33
|
方法は二つあります。
|
34
34
|
0. コードをハイライトした状態で<code>ボタンを押す
|
35
|
-
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)
|
35
|
+
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)
|
36
|
+
|
37
|
+
書いてみた
|
38
|
+
---
|
39
|
+
部分的ですが、参考までに。
|
40
|
+
|
41
|
+
**main.py**
|
42
|
+
```Python
|
43
|
+
from command import eto_command, choice_command
|
44
|
+
|
45
|
+
command_dict = {
|
46
|
+
'干支': eto_command,
|
47
|
+
'選ぶ': choice_command,
|
48
|
+
}
|
49
|
+
|
50
|
+
def interpret_command(input_str):
|
51
|
+
command, *args = input_str.split()
|
52
|
+
|
53
|
+
if command in command_dict:
|
54
|
+
return command_dict[command](*args)
|
55
|
+
|
56
|
+
return f'「{command}」の意味がわかりません。'
|
57
|
+
|
58
|
+
|
59
|
+
def main():
|
60
|
+
while True:
|
61
|
+
input_str = input('なんか入力してね: ')
|
62
|
+
|
63
|
+
if 'さようなら' == input_str:
|
64
|
+
print('了解。システムを終了します。')
|
65
|
+
break
|
66
|
+
|
67
|
+
print(
|
68
|
+
interpret_command(input_str), end='\n\n'
|
69
|
+
)
|
70
|
+
|
71
|
+
|
72
|
+
if __name__ == '__main__':
|
73
|
+
main()
|
74
|
+
```
|
75
|
+
|
76
|
+
**command.py**
|
77
|
+
```Python
|
78
|
+
import random
|
79
|
+
|
80
|
+
|
81
|
+
def eto_command(year_str=None):
|
82
|
+
if year_str is None:
|
83
|
+
return 'コマンドと一緒に西暦も入力してね。'
|
84
|
+
|
85
|
+
try:
|
86
|
+
year = int(year_str)
|
87
|
+
except ValueError:
|
88
|
+
return f'{year_str}は西暦だと解釈できないよ。'
|
89
|
+
|
90
|
+
eto = [
|
91
|
+
"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"
|
92
|
+
][(year + 8) % 12]
|
93
|
+
|
94
|
+
return f'{year}年生まれの干支は「{eto}」です。'
|
95
|
+
|
96
|
+
|
97
|
+
def choice_command(*candidate_list):
|
98
|
+
if not candidate_list:
|
99
|
+
return 'そもそも選択肢がないよ。'
|
100
|
+
|
101
|
+
choiced = random.choice(candidate_list)
|
102
|
+
return f'「{choiced}」が選ばれました。'
|
103
|
+
```
|
104
|
+
|
105
|
+
**実行例** おまけ:[Wandbox](https://wandbox.org/permlink/4rd3yuofaGzaTGqd)
|
106
|
+
```plain
|
107
|
+
なんか入力してね: 干支
|
108
|
+
コマンドと一緒に西暦も入力してね。
|
109
|
+
|
110
|
+
なんか入力してね: 干支 2018
|
111
|
+
2018年生まれの干支は「戌」です。
|
112
|
+
|
113
|
+
なんか入力してね: 干支 3.14
|
114
|
+
3.14は西暦だと解釈できないよ。
|
115
|
+
|
116
|
+
なんか入力してね: 選ぶ
|
117
|
+
そもそも選択肢がないよ。
|
118
|
+
|
119
|
+
なんか入力してね: 選ぶ 1 2 3
|
120
|
+
「2」が選ばれました。
|
121
|
+
|
122
|
+
なんか入力してね: 選ぶ 1 2 3
|
123
|
+
「2」が選ばれました。
|
124
|
+
|
125
|
+
なんか入力してね: 選ぶ 1 2 3
|
126
|
+
「1」が選ばれました。
|
127
|
+
|
128
|
+
なんか入力してね: 面白いこと言って
|
129
|
+
「面白いこと言って」の意味がわかりません。
|
130
|
+
|
131
|
+
なんか入力してね: さようなら
|
132
|
+
了解。システムを終了します。
|
133
|
+
```
|
1
追記
answer
CHANGED
@@ -17,4 +17,19 @@
|
|
17
17
|
基本的には**書いたとおりにしか動いていない**ので、関数の呼び出し方にご留意ください。
|
18
18
|
また、例外の安易な握り潰しは絶対にしないようにしてください。
|
19
19
|
|
20
|
-
Wandboxにコードを起こして再現させたので、一応共有しておきます。[Wandbox](https://wandbox.org/permlink/v0rKWG2DotBLV2un)
|
20
|
+
Wandboxにコードを起こして再現させたので、一応共有しておきます。[Wandbox](https://wandbox.org/permlink/v0rKWG2DotBLV2un)
|
21
|
+
|
22
|
+
コードブロックの適用の仕方
|
23
|
+
---
|
24
|
+
次のようにコードブロックを利用出来ます。
|
25
|
+
```Python
|
26
|
+
def main():
|
27
|
+
print('Hello World!')
|
28
|
+
|
29
|
+
if __name__ == __main__:
|
30
|
+
main()
|
31
|
+
```
|
32
|
+
|
33
|
+
方法は二つあります。
|
34
|
+
0. コードをハイライトした状態で<code>ボタンを押す
|
35
|
+
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)
|