質問編集履歴

2

importのコードの追加をしました。

2018/03/09 07:57

投稿

goliragolira
goliragolira

スコア26

test CHANGED
File without changes
test CHANGED
@@ -194,31 +194,167 @@
194
194
 
195
195
 
196
196
 
197
- 干支”のコード
197
+ import_eto.py”のコード
198
198
 
199
199
 
200
200
 
201
201
  #coding:utf-8
202
202
 
203
- year str = input('あなたの生まれた年を西暦で入力してください')
204
-
205
- year = int(year star)
206
-
207
- number of eto = (year + 8) % 12
208
-
209
- eto tuple = ("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥")
210
-
211
- eto name = eto tuple[number of eto]
212
-
213
- print(”あなたの干支は”, eto name, "です")
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
203
+
204
+
205
+ def eto_command(command):
206
+
207
+ _ eto, year = command.split()
208
+
209
+ _ eto_list = ("子", "丑", "寅", "卯", "辰", "巳", "午",
210
+
211
+ _ "未", "申", "酉", "戌", "亥")
212
+
213
+ _ eto_number = (int(year) + 8) % 12
214
+
215
+ _ eto_name = eto_list[eto_number]
216
+
217
+
218
+
219
+ _ response = "{}年生まれの干支は「{}」です".format(year, eto_name)
220
+
221
+ _ return response
222
+
223
+
224
+
225
+ "my_random.py"のコード
226
+
227
+
228
+
229
+ import random
230
+
231
+
232
+
233
+ def choice_command(command):
234
+
235
+ _ data = command.split()
236
+
237
+ _ choiced = random.choice(data[1:])
238
+
239
+ _ response = "「{}」が選ばれました".format(choiced)
240
+
241
+ _ return response
242
+
243
+
244
+
245
+ def dice_command(command):
246
+
247
+ _ num = random.randrange(1, 100)
248
+
249
+ _ response = "「{}」の目が出ました".format(num)
250
+
251
+ _ return response
252
+
253
+
254
+
255
+
256
+
257
+ "import_hiduke.py"のコード
258
+
259
+
260
+
261
+ rom datetime import date, datetime
262
+
263
+
264
+
265
+ def kyou_command():
266
+
267
+ _ today = date.today()
268
+
269
+ _ response = "今日の日付は{}です".format(today)
270
+
271
+ _ return response
272
+
273
+
274
+
275
+ def ima_command():
276
+
277
+ _ now = datetime.now()
278
+
279
+ _ response = "現在日時は{}です".format(now)
280
+
281
+ _ return response
282
+
283
+
284
+
285
+ def youbi_command(command):
286
+
287
+ _ try:
288
+
289
+ _ data = command.split()
290
+
291
+ _ year = int(data[1])
292
+
293
+ _ month = int(data[2])
294
+
295
+ _ day = int(data[3])
296
+
297
+ _ one_day = date(year, month, day)
298
+
299
+
300
+
301
+ _ weekday_str = "月火水木金土日"
302
+
303
+ _ weekday = weekday_str[one_day.weekday()]
304
+
305
+
306
+
307
+ _ response = "{}は、{}曜日です".format(one_day, weekday)
308
+
309
+
310
+
311
+ _ except IndexError:
312
+
313
+ _ response = "3つの値(年月日)を空白で区切って入力して下さい"
314
+
315
+ _ except ValueError:
316
+
317
+ _ response = "正しい日付を入力して下さい"
318
+
319
+
320
+
321
+ _ return response
322
+
323
+
324
+
325
+
326
+
327
+ "forecast.py"のコード
328
+
329
+
330
+
331
+ import requests
332
+
333
+
334
+
335
+ def weather_command():
336
+
337
+ _ base_url = "http://weather.livedoor.com/forecast/webservice/json/v1"
338
+
339
+ _ city_code = "230010"
340
+
341
+ _ url = "{}?city={}".format(base_url, city_code)
342
+
343
+ _ r = requests.get(url)
344
+
345
+ _ weather_data = r.json()
346
+
347
+ _ city = weather_data["location"]["city"]
348
+
349
+ _ label = weather_data["forecasts"][0]["dateLabel"]
350
+
351
+ _ telop = weather_data["forecasts"][0]["telop"]
352
+
353
+
354
+
355
+ _ response = "{}の{}の天気は「{}」です".format(city, label, telop)
356
+
357
+ _ return response
222
358
 
223
359
 
224
360
 

1

インデントを見やすく変更しました

2018/03/09 07:57

投稿

goliragolira
goliragolira

スコア26

test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,8 @@
10
10
 
11
11
 
12
12
 
13
+
14
+
13
15
  1:プログラムコード
14
16
 
15
17
 
@@ -28,43 +30,43 @@
28
30
 
29
31
  def len_command(command):
30
32
 
31
- cmd, text = command.split()
33
+ _ cmd, text = command.split()
32
-
34
+
33
- length = len(text)
35
+ _ length = len(text)
34
-
36
+
35
- response = "文字列の長さは{}です".format(length)
37
+ _ response = "文字列の長さは{}です".format(length)
36
-
38
+
37
- return response
39
+ _ return response
38
40
 
39
41
 
40
42
 
41
43
  def heisei_command(command):
42
44
 
43
- heisei, year_str = command.split()
45
+ _ heisei, year_str = command.split()
44
-
46
+
45
- if year_str.isdigit():
47
+ _ if year_str.isdigit():
46
-
48
+
47
- year = int(year_str)
49
+ _ year = int(year_str)
48
-
49
-
50
-
50
+
51
+
52
+
51
- if year >= 1989:
53
+ _ if year >= 1989:
52
-
54
+
53
- heisei_year = year - 1988
55
+ _ heisei_year = year - 1988
54
-
56
+
55
- response = "西暦{}年は、平成{}年です".format(year, heisei_year)
57
+ _ response = "西暦{}年は、平成{}年です".format(year, heisei_year)
56
-
58
+
57
- else:
59
+ _ else:
58
-
60
+
59
- response = "西暦{}年は、平成ではありません".format(year)
61
+ _ response = "西暦{}年は、平成ではありません".format(year)
60
-
61
-
62
-
62
+
63
+
64
+
63
- else:
65
+ _ else:
64
-
66
+
65
- response = "数値を指定して下さい"
67
+ _ response = "数値を指定して下さい"
66
-
68
+
67
- return response
69
+ _ return response
68
70
 
69
71
 
70
72
 
@@ -82,101 +84,101 @@
82
84
 
83
85
  for line in lines:
84
86
 
85
- word_list = line.split(",")
87
+ _ word_list = line.split(",")
86
-
88
+
87
- key = word_list[0]
89
+ _ key = word_list[0]
88
-
90
+
89
- response = word_list[1]
91
+ _ response = word_list[1]
90
-
92
+
91
- bot_dict[key] = response
93
+ _ bot_dict[key] = response
92
94
 
93
95
 
94
96
 
95
97
  while True:
96
98
 
97
- command = input("なんか言え ")
98
-
99
- response = ""
100
-
101
- try:
102
-
103
- for key in bot_dict:
104
-
105
- if key in command:
106
-
107
- response = bot_dict[key]
108
-
109
- break
110
-
111
-
112
-
113
- if "平成" in command:
114
-
115
- response = heisei_command(command)
116
-
117
-
118
-
119
- if "長さ" in command:
120
-
121
- response = len_command(command)
122
-
123
-
124
-
125
- if "干支" in command:
126
-
127
- response = eto_command(command)
128
-
129
-
130
-
131
- if "選ぶ" in command:
132
-
133
- response = choice_command(command)
134
-
135
-
136
-
137
- if "サイコロ" in command:
138
-
139
- response = dice_command(command)
140
-
141
-
142
-
143
- if "今日" in command:
144
-
145
- response = kyou_command()
146
-
147
-
148
-
149
- if "現在" in command:
150
-
151
- response = ima_command()
152
-
153
-
154
-
155
- if "曜日" in command:
156
-
157
- response = youbi_command(command)
158
-
159
-
160
-
161
- if "天気" in command:
162
-
163
- response = weather_command()
164
-
165
- if not response:
166
-
167
- response = "日本語喋れや豚野郎"
168
-
169
-
170
-
171
- print(response)
172
-
173
-
174
-
175
- if "さようなら" in command:
176
-
177
- print("二度と開くなよまじで")
178
-
179
- break
99
+ _ command = input("なんか言え ")
100
+
101
+ _ response = ""
102
+
103
+ _ try:
104
+
105
+ _ for key in bot_dict:
106
+
107
+ _ if key in command:
108
+
109
+ _ response = bot_dict[key]
110
+
111
+ _ break
112
+
113
+
114
+
115
+ _ if "平成" in command:
116
+
117
+ _ response = heisei_command(command)
118
+
119
+
120
+
121
+ _ if "長さ" in command:
122
+
123
+ _ response = len_command(command)
124
+
125
+
126
+
127
+ _ if "干支" in command:
128
+
129
+ _ response = eto_command(command)
130
+
131
+
132
+
133
+ _ if "選ぶ" in command:
134
+
135
+ _ response = choice_command(command)
136
+
137
+
138
+
139
+ _ if "サイコロ" in command:
140
+
141
+ _ response = dice_command(command)
142
+
143
+
144
+
145
+ _ if "今日" in command:
146
+
147
+ _ response = kyou_command()
148
+
149
+
150
+
151
+ _ if "現在" in command:
152
+
153
+ _ response = ima_command()
154
+
155
+
156
+
157
+ _ if "曜日" in command:
158
+
159
+ _ response = youbi_command(command)
160
+
161
+
162
+
163
+ _ if "天気" in command:
164
+
165
+ _ response = weather_command()
166
+
167
+ _ if not response:
168
+
169
+ _ response = "日本語喋れや豚野郎"
170
+
171
+
172
+
173
+ _ print(response)
174
+
175
+
176
+
177
+ _ if "さようなら" in command:
178
+
179
+ _ print("二度と開くなよまじで")
180
+
181
+ _ break
180
182
 
181
183
 
182
184
 
@@ -192,6 +194,40 @@
192
194
 
193
195
 
194
196
 
197
+ ”干支”のコード
198
+
199
+
200
+
201
+ #coding:utf-8
202
+
203
+ year str = input('あなたの生まれた年を西暦で入力してください')
204
+
205
+ year = int(year star)
206
+
207
+ number of eto = (year + 8) % 12
208
+
209
+ eto tuple = ("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥")
210
+
211
+ eto name = eto tuple[number of eto]
212
+
213
+ print(”あなたの干支は”, eto name, "です")
214
+
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+
195
231
  2:エラーコード
196
232
 
197
233