質問編集履歴

7

期待値が出力されたソースの修正

2020/03/19 05:58

投稿

reok
reok

スコア5

test CHANGED
File without changes
test CHANGED
@@ -68,27 +68,27 @@
68
68
 
69
69
  def post():
70
70
 
71
- radio = {"勝利": "グー","負け": "チョキ","引き分け": "パー"}
71
+ radio = {"1": "グー","2": "チョキ","3": "パー"}
72
-
72
+
73
- janken = request.form.get('janken')
73
+ user = request.form.get('janken')
74
-
75
-
76
-
74
+
75
+
76
+
77
- user_choice = radio[janken]
77
+ user_choice = radio[user]
78
78
 
79
79
 
80
80
 
81
81
  choice_list = ["1", "2", "3"]
82
82
 
83
- pc = janken[random.choice(choice_list)]
83
+ pc = radio[random.choice(choice_list)]
84
-
85
-
86
-
84
+
85
+
86
+
87
- draw = '<font color="#32CD32">DRAW</font>'
87
+ draw = 'DRAW'
88
-
88
+
89
- win = '<font color="#FF7F50">You Win!!</font>'
89
+ win = 'You Win!!'
90
-
90
+
91
- lose = '<font color="#0000FF">You lose!!</font>'
91
+ lose = 'You lose!!'
92
92
 
93
93
 
94
94
 
@@ -136,9 +136,13 @@
136
136
 
137
137
 
138
138
 
139
- return render_template('index.html',
139
+ return render_template('index.html', \
140
+
140
-
141
+ title = 'Form Sample(get)', \
142
+
141
- radio=radio)
143
+ message = "結果は%s" % judge ,radio=radio,judge=judge)
144
+
145
+
142
146
 
143
147
  if __name__ == '__main__':
144
148
 
@@ -164,7 +168,7 @@
164
168
 
165
169
  <label for="r1">グー:</label>
166
170
 
167
- <input type="radio" id="r1" name="janken" value="勝利">
171
+ <input type="radio" id="r1" name="janken" value="1">
168
172
 
169
173
  </div>
170
174
 
@@ -172,7 +176,7 @@
172
176
 
173
177
  <label for="r2">チョキ:</label>
174
178
 
175
- <input type="radio" id="r2" name="janken" value="負け">
179
+ <input type="radio" id="r2" name="janken" value="2">
176
180
 
177
181
  </div>
178
182
 
@@ -180,7 +184,7 @@
180
184
 
181
185
  <label for="r3">パー:</label>
182
186
 
183
- <input type="radio" id="r3" name="janken" value="引き分け">
187
+ <input type="radio" id="r3" name="janken" value="3">
184
188
 
185
189
  </div>
186
190
 

6

ソースとエラーの出力結果を修正

2020/03/19 05:58

投稿

reok
reok

スコア5

test CHANGED
File without changes
test CHANGED
@@ -28,15 +28,123 @@
28
28
 
29
29
  ---エラーコード---
30
30
 
31
- File "c:/Users/test/server.py", line 16, in post
31
+ File "c:/Users/test/server.py", line 19, in post
32
+
33
+ pc = janken[random.choice(choice_list)]
34
+
35
+ TypeError: string indices must be integers
36
+
37
+ ```
38
+
39
+
40
+
41
+ ### 該当のソースコード
42
+
43
+
44
+
45
+ ```
46
+
47
+ from flask import Flask, render_template, request
48
+
49
+ import random
50
+
51
+
52
+
53
+ app = Flask(__name__)
54
+
55
+ @app.route('/', methods=['GET'])
56
+
57
+ def get():
58
+
59
+ return render_template('index.html', \
60
+
61
+ title = 'Form Sample(get)', \
62
+
63
+ message = 'じゃんけんをはじめます')
64
+
65
+
66
+
67
+ @app.route('/', methods=['POST'])
68
+
69
+ def post():
70
+
71
+ radio = {"勝利": "グー","負け": "チョキ","引き分け": "パー"}
72
+
73
+ janken = request.form.get('janken')
74
+
75
+
32
76
 
33
77
  user_choice = radio[janken]
34
78
 
79
+
80
+
81
+ choice_list = ["1", "2", "3"]
82
+
83
+ pc = janken[random.choice(choice_list)]
84
+
85
+
86
+
87
+ draw = '<font color="#32CD32">DRAW</font>'
88
+
89
+ win = '<font color="#FF7F50">You Win!!</font>'
90
+
91
+ lose = '<font color="#0000FF">You lose!!</font>'
92
+
93
+
94
+
95
+ if user_choice == pc:
96
+
97
+ judge = draw
98
+
99
+ else:
100
+
101
+ if user_choice == "グー":
102
+
103
+ if pc == "チョキ":
104
+
105
+ judge = win
106
+
107
+ else:
108
+
109
+ judge = lose
110
+
111
+
112
+
113
+ elif user_choice == "チョキ":
114
+
115
+ if pc == "パー":
116
+
117
+ judge = win
118
+
119
+ else:
120
+
121
+ judge = lose
122
+
123
+
124
+
125
+ else:
126
+
127
+ if pc == "グー":
128
+
129
+ judge = win
130
+
131
+ else:
132
+
133
+ judge = lose
134
+
135
+
136
+
137
+
138
+
139
+ return render_template('index.html',
140
+
141
+ radio=radio)
142
+
35
- KeyError: '勝利'
143
+ if __name__ == '__main__':
144
+
36
-
145
+ app.run()
146
+
37
- ```
147
+ ```
38
-
39
-
40
148
 
41
149
  ### 該当のソースコード
42
150
 
@@ -44,114 +152,6 @@
44
152
 
45
153
  ```
46
154
 
47
- from flask import Flask, render_template, request
48
-
49
- import random
50
-
51
-
52
-
53
- app = Flask(__name__)
54
-
55
- @app.route('/', methods=['GET'])
56
-
57
- def get():
58
-
59
- return render_template('index.html', \
60
-
61
- title = 'Form Sample(get)', \
62
-
63
- message = 'じゃんけんをはじめます')
64
-
65
-
66
-
67
- @app.route('/', methods=['POST'])
68
-
69
- def post():
70
-
71
- radio = {"1": "グー","2": "チョキ","3": "パー"}
72
-
73
- janken = request.form.get('janken')
74
-
75
-
76
-
77
- user_choice = radio[janken]
78
-
79
-
80
-
81
- choice_list = ["1", "'2", "3"]
82
-
83
- pc = janken[random.choice(choice_list)]
84
-
85
-
86
-
87
- draw = '<font color="#32CD32">DRAW</font>'
88
-
89
- win = '<font color="#FF7F50">You Win!!</font>'
90
-
91
- lose = '<font color="#0000FF">You lose!!</font>'
92
-
93
-
94
-
95
- if user_choice == pc:
96
-
97
- judge = draw
98
-
99
- else:
100
-
101
- if user_choice == "グー":
102
-
103
- if pc == "チョキ":
104
-
105
- judge = win
106
-
107
- else:
108
-
109
- judge = lose
110
-
111
-
112
-
113
- elif user_choice == "チョキ":
114
-
115
- if pc == "パー":
116
-
117
- judge = win
118
-
119
- else:
120
-
121
- judge = lose
122
-
123
-
124
-
125
- else:
126
-
127
- if pc == "グー":
128
-
129
- judge = win
130
-
131
- else:
132
-
133
- judge = lose
134
-
135
-
136
-
137
-
138
-
139
- return render_template('index.html',
140
-
141
- radio=radio)
142
-
143
- if __name__ == '__main__':
144
-
145
- app.run()
146
-
147
- ```
148
-
149
- ### 該当のソースコード
150
-
151
-
152
-
153
- ```
154
-
155
155
  <body>
156
156
 
157
157
  <h1>{{ title }}</h1>

5

エラー出力の修正

2020/03/19 01:31

投稿

reok
reok

スコア5

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  Python3でflaskを利用してWEBでじゃんけんゲームを実行するプログラム
8
8
 
9
- を作成したのでpostで受け取るgetの2画面目のプログラムを実行する部分がどうしても
9
+ を作成したのでpostで受け取るgetの遷移後のプログラムを実行する部分がどうしても
10
10
 
11
11
  上手く実行できない為添削をお願いします。
12
12
 
@@ -18,11 +18,21 @@
18
18
 
19
19
  ```
20
20
 
21
+ ---起きている事象---
22
+
23
+ プルダウンのグーを選択後、じゃんけんぽんのボタンsubmit後
24
+
25
+ Internal Server Error となってしまいます。
26
+
27
+
28
+
29
+ ---エラーコード---
30
+
21
31
  File "c:/Users/test/server.py", line 16, in post
22
32
 
23
33
  user_choice = radio[janken]
24
34
 
25
- KeyError: 'on'
35
+ KeyError: '勝利'
26
36
 
27
37
  ```
28
38
 
@@ -188,7 +198,11 @@
188
198
 
189
199
  ### 試したこと
190
200
 
201
+ 1画面目までは表示しますが2画面目以降の
202
+
191
- 1画面目までは表示しますが2画面目の実行箇所にてエラーになってしまいます。
203
+ 実行箇所にてエラーになってしまいます。
204
+
205
+
192
206
 
193
207
 
194
208
 

4

htmlのvalue属性修正

2020/03/19 00:14

投稿

reok
reok

スコア5

test CHANGED
File without changes
test CHANGED
@@ -154,7 +154,7 @@
154
154
 
155
155
  <label for="r1">グー:</label>
156
156
 
157
- <input type="radio" id="r1" name="janken" value1="勝">
157
+ <input type="radio" id="r1" name="janken" value="勝">
158
158
 
159
159
  </div>
160
160
 
@@ -162,7 +162,7 @@
162
162
 
163
163
  <label for="r2">チョキ:</label>
164
164
 
165
- <input type="radio" id="r2" name="janken" value2="負け">
165
+ <input type="radio" id="r2" name="janken" value="負け">
166
166
 
167
167
  </div>
168
168
 
@@ -170,7 +170,7 @@
170
170
 
171
171
  <label for="r3">パー:</label>
172
172
 
173
- <input type="radio" id="r3" name="janken" value3="引き分け">
173
+ <input type="radio" id="r3" name="janken" value="引き分け">
174
174
 
175
175
  </div>
176
176
 

3

ソースの修正、発生しているエラーの修正

2020/03/19 00:02

投稿

reok
reok

スコア5

test CHANGED
File without changes
test CHANGED
@@ -20,7 +20,9 @@
20
20
 
21
21
  File "c:/Users/test/server.py", line 16, in post
22
22
 
23
- user_choice = radio[name]
23
+ user_choice = radio[janken]
24
+
25
+ KeyError: 'on'
24
26
 
25
27
  ```
26
28
 

2

ソースの修正

2020/03/18 10:13

投稿

reok
reok

スコア5

test CHANGED
File without changes
test CHANGED
@@ -44,11 +44,11 @@
44
44
 
45
45
  def get():
46
46
 
47
- return render_template('index.html', \
47
+ return render_template('index.html', \
48
48
 
49
- title = 'Form Sample(get)', \
49
+ title = 'Form Sample(get)', \
50
50
 
51
- message = 'じゃんけんをはじめます')
51
+ message = 'じゃんけんをはじめます')
52
52
 
53
53
 
54
54
 
@@ -58,17 +58,17 @@
58
58
 
59
59
  radio = {"1": "グー","2": "チョキ","3": "パー"}
60
60
 
61
- name = request.form.get('radio')
61
+ janken = request.form.get('janken')
62
62
 
63
63
 
64
64
 
65
- user_choice = radio[name]
65
+ user_choice = radio[janken]
66
66
 
67
67
 
68
68
 
69
69
  choice_list = ["1", "'2", "3"]
70
70
 
71
- pc = name[random.choice(choice_list)]
71
+ pc = janken[random.choice(choice_list)]
72
72
 
73
73
 
74
74
 
@@ -122,17 +122,15 @@
122
122
 
123
123
 
124
124
 
125
- name = request.form.get('radio')
126
125
 
127
-
128
126
 
129
127
  return render_template('index.html',
130
128
 
131
- name=name, radio=radio)
129
+ radio=radio)
132
130
 
133
131
  if __name__ == '__main__':
134
132
 
135
- app.run()
133
+ app.run()
136
134
 
137
135
  ```
138
136
 
@@ -144,43 +142,43 @@
144
142
 
145
143
  <body>
146
144
 
147
- <h1>{{ title }}</h1>
145
+ <h1>{{ title }}</h1>
148
146
 
149
- <p>{{ message }}</p>
147
+ <p>{{ message }}</p>
150
148
 
151
- <form action="/" method="POST" enctype="multipart/form-data">
149
+ <form action="/" method="POST" enctype="multipart/form-data">
152
150
 
153
- <div>
151
+ <div>
154
152
 
155
- <label for="r1">グー:</label>
153
+ <label for="r1">グー:</label>
156
154
 
157
- <input type="radio" id="r1" name_1="radio1" value1="勝ち">
155
+ <input type="radio" id="r1" name="janken" value1="勝ち">
158
156
 
159
- </div>
157
+ </div>
160
158
 
161
- <div>
159
+ <div>
162
160
 
163
- <label for="r2">チョキ:</label>
161
+ <label for="r2">チョキ:</label>
164
162
 
165
- <input type="radio" id="r2" name_2="radio2" value2="負け">
163
+ <input type="radio" id="r2" name="janken" value2="負け">
166
164
 
167
- </div>
165
+ </div>
168
166
 
169
- <div>
167
+ <div>
170
168
 
171
- <label for="r3">パー:</label>
169
+ <label for="r3">パー:</label>
172
170
 
173
- <input type="radio" id="r3" name_3="radio3" value3="引き分け">
171
+ <input type="radio" id="r3" name="janken" value3="引き分け">
174
172
 
175
- </div>
173
+ </div>
176
174
 
177
- <div>
175
+ <div>
178
176
 
179
- <input type="submit" value="じゃんけんぽん!">
177
+ <input type="submit" value="じゃんけんぽん!">
180
178
 
181
- </div>
179
+ </div>
182
180
 
183
- </form>
181
+ </form>
184
182
 
185
183
  </body>
186
184
 

1

ソースの文法と発生エラーメッセージの修正

2020/03/18 10:12

投稿

reok
reok

スコア5

test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,9 @@
18
18
 
19
19
  ```
20
20
 
21
- 気をつけた方がいい箇所が自力では見つけきれないので、出来るだけ頑張って書きました、添削をできればお願いします。
21
+ File "c:/Users/test/server.py", line 16, in post
22
+
23
+ user_choice = radio[name]
22
24
 
23
25
  ```
24
26
 
@@ -29,8 +31,6 @@
29
31
 
30
32
 
31
33
  ```
32
-
33
- @zyanken.py
34
34
 
35
35
  from flask import Flask, render_template, request
36
36
 
@@ -52,79 +52,83 @@
52
52
 
53
53
 
54
54
 
55
- @app.route('/zyanken', methods=['POST'])
55
+ @app.route('/', methods=['POST'])
56
56
 
57
57
  def post():
58
58
 
59
+ radio = {"1": "グー","2": "チョキ","3": "パー"}
60
+
61
+ name = request.form.get('radio')
59
62
 
60
63
 
61
64
 
62
-
63
- radio = {"1": "グー","2": "チョキ","3": "パー"}
64
-
65
- name = request.form.get('radio')
65
+ user_choice = radio[name]
66
66
 
67
67
 
68
68
 
69
+ choice_list = ["1", "'2", "3"]
70
+
69
- user_choice = radio[name]
71
+ pc = name[random.choice(choice_list)]
70
72
 
71
73
 
72
74
 
73
- choice_list = ["1", "'2", "3"]
75
+ draw = '<font color="#32CD32">DRAW</font>'
74
76
 
75
- pc = name[random.choice(choice_list)]
77
+ win = '<font color="#FF7F50">You Win!!</font>'
78
+
79
+ lose = '<font color="#0000FF">You lose!!</font>'
76
80
 
77
81
 
78
82
 
79
- if user_choice == pc:
83
+ if user_choice == pc:
80
84
 
81
- judge = radio_3
85
+ judge = draw
82
86
 
83
- else:
87
+ else:
84
88
 
85
- if user_choice == "グー":
89
+ if user_choice == "グー":
86
90
 
87
- if pc == "チョキ":
91
+ if pc == "チョキ":
88
92
 
93
+ judge = win
94
+
95
+ else:
96
+
89
- judge = name_1
97
+ judge = lose
98
+
99
+
100
+
101
+ elif user_choice == "チョキ":
102
+
103
+ if pc == "パー":
104
+
105
+ judge = win
106
+
107
+ else:
108
+
109
+ judge = lose
110
+
111
+
90
112
 
91
113
  else:
92
114
 
115
+ if pc == "グー":
116
+
117
+ judge = win
118
+
119
+ else:
120
+
93
- judge = name_2
121
+ judge = lose
94
122
 
95
123
 
96
124
 
97
- elif user_choice == "チョキ":
98
-
99
- if pc == "パー":
100
-
101
- judge = name_1
102
-
103
- else:
104
-
105
- judge = name_2
106
-
107
-
108
-
109
- else:
110
-
111
- if pc == "グー":
112
-
113
- judge = name_1
114
-
115
- else:
116
-
117
- judge = name_2
118
-
119
-
120
-
121
- name = request.form.get('radio' )
125
+ name = request.form.get('radio')
122
126
 
123
127
 
124
128
 
125
- return render_template('index.html',
129
+ return render_template('index.html',
126
130
 
127
- name=name, title=title)
131
+ name=name, radio=radio)
128
132
 
129
133
  if __name__ == '__main__':
130
134
 
@@ -138,8 +142,6 @@
138
142
 
139
143
  ```
140
144
 
141
- @index.html
142
-
143
145
  <body>
144
146
 
145
147
  <h1>{{ title }}</h1>
@@ -152,7 +154,7 @@
152
154
 
153
155
  <label for="r1">グー:</label>
154
156
 
155
- <input type="radio1" id="r1" name_1="radio1" value1="勝ち">
157
+ <input type="radio" id="r1" name_1="radio1" value1="勝ち">
156
158
 
157
159
  </div>
158
160
 
@@ -160,15 +162,15 @@
160
162
 
161
163
  <label for="r2">チョキ:</label>
162
164
 
163
- <input type="radio2" id="r2" name_2="radio2" value2="負け">
165
+ <input type="radio" id="r2" name_2="radio2" value2="負け">
164
166
 
165
167
  </div>
166
168
 
167
- <div>
169
+ <div>
168
170
 
169
171
  <label for="r3">パー:</label>
170
172
 
171
- <input type="radio3" id="r3" name_3="radio3" value3="引き分け">
173
+ <input type="radio" id="r3" name_3="radio3" value3="引き分け">
172
174
 
173
175
  </div>
174
176