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

質問編集履歴

7

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

2020/03/19 05:58

投稿

reok
reok

スコア5

title CHANGED
File without changes
body CHANGED
@@ -33,17 +33,17 @@
33
33
 
34
34
  @app.route('/', methods=['POST'])
35
35
  def post():
36
- radio = {"勝利": "グー","負け": "チョキ","引き分け": "パー"}
36
+ radio = {"1": "グー","2": "チョキ","3": "パー"}
37
- janken = request.form.get('janken')
37
+ user = request.form.get('janken')
38
38
 
39
- user_choice = radio[janken]
39
+ user_choice = radio[user]
40
40
 
41
41
  choice_list = ["1", "2", "3"]
42
- pc = janken[random.choice(choice_list)]
42
+ pc = radio[random.choice(choice_list)]
43
43
 
44
- draw = '<font color="#32CD32">DRAW</font>'
44
+ draw = 'DRAW'
45
- win = '<font color="#FF7F50">You Win!!</font>'
45
+ win = 'You Win!!'
46
- lose = '<font color="#0000FF">You lose!!</font>'
46
+ lose = 'You lose!!'
47
47
 
48
48
  if user_choice == pc:
49
49
  judge = draw
@@ -67,8 +67,10 @@
67
67
  judge = lose
68
68
 
69
69
 
70
- return render_template('index.html',
70
+ return render_template('index.html', \
71
+ title = 'Form Sample(get)', \
71
- radio=radio)
72
+ message = "結果は%s" % judge ,radio=radio,judge=judge)
73
+
72
74
  if __name__ == '__main__':
73
75
  app.run()
74
76
  ```
@@ -81,15 +83,15 @@
81
83
  <form action="/" method="POST" enctype="multipart/form-data">
82
84
  <div>
83
85
  <label for="r1">グー:</label>
84
- <input type="radio" id="r1" name="janken" value="勝利">
86
+ <input type="radio" id="r1" name="janken" value="1">
85
87
  </div>
86
88
  <div>
87
89
  <label for="r2">チョキ:</label>
88
- <input type="radio" id="r2" name="janken" value="負け">
90
+ <input type="radio" id="r2" name="janken" value="2">
89
91
  </div>
90
92
  <div>
91
93
  <label for="r3">パー:</label>
92
- <input type="radio" id="r3" name="janken" value="引き分け">
94
+ <input type="radio" id="r3" name="janken" value="3">
93
95
  </div>
94
96
  <div>
95
97
  <input type="submit" value="じゃんけんぽん!">

6

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

2020/03/19 05:58

投稿

reok
reok

スコア5

title CHANGED
File without changes
body CHANGED
@@ -13,9 +13,9 @@
13
13
  Internal Server Error となってしまいます。
14
14
 
15
15
  ---エラーコード---
16
- File "c:/Users/test/server.py", line 16, in post
16
+ File "c:/Users/test/server.py", line 19, in post
17
- user_choice = radio[janken]
17
+ pc = janken[random.choice(choice_list)]
18
- KeyError: '勝利'
18
+ TypeError: string indices must be integers
19
19
  ```
20
20
 
21
21
  ### 該当のソースコード
@@ -33,12 +33,12 @@
33
33
 
34
34
  @app.route('/', methods=['POST'])
35
35
  def post():
36
- radio = {"1": "グー","2": "チョキ","3": "パー"}
36
+ radio = {"勝利": "グー","負け": "チョキ","引き分け": "パー"}
37
37
  janken = request.form.get('janken')
38
38
 
39
39
  user_choice = radio[janken]
40
40
 
41
- choice_list = ["1", "'2", "3"]
41
+ choice_list = ["1", "2", "3"]
42
42
  pc = janken[random.choice(choice_list)]
43
43
 
44
44
  draw = '<font color="#32CD32">DRAW</font>'

5

エラー出力の修正

2020/03/19 01:31

投稿

reok
reok

スコア5

title CHANGED
File without changes
body CHANGED
@@ -2,15 +2,20 @@
2
2
 
3
3
  ここに質問の内容を詳しく書いてください。
4
4
  Python3でflaskを利用してWEBでじゃんけんゲームを実行するプログラム
5
- を作成したのでpostで受け取るgetの2画面目のプログラムを実行する部分がどうしても
5
+ を作成したのでpostで受け取るgetの遷移後のプログラムを実行する部分がどうしても
6
6
  上手く実行できない為添削をお願いします。
7
7
 
8
8
  ### 発生している問題・エラーメッセージ
9
9
 
10
10
  ```
11
+ ---起きている事象---
12
+ プルダウンのグーを選択後、じゃんけんぽんのボタンsubmit後
13
+ Internal Server Error となってしまいます。
14
+
15
+ ---エラーコード---
11
16
  File "c:/Users/test/server.py", line 16, in post
12
17
  user_choice = radio[janken]
13
- KeyError: 'on'
18
+ KeyError: '勝利'
14
19
  ```
15
20
 
16
21
  ### 該当のソースコード
@@ -93,7 +98,9 @@
93
98
  </body>
94
99
  ```
95
100
  ### 試したこと
101
+ 1画面目までは表示しますが2画面目以降の
96
- 1画面目までは表示しますが2画面目の実行箇所にてエラーになってしまいます。
102
+ 実行箇所にてエラーになってしまいます。
97
103
 
104
+
98
105
  ### 補足情報(FW/ツールのバージョンなど)
99
106
  Python3.7.7

4

htmlのvalue属性修正

2020/03/19 00:14

投稿

reok
reok

スコア5

title CHANGED
File without changes
body CHANGED
@@ -76,15 +76,15 @@
76
76
  <form action="/" method="POST" enctype="multipart/form-data">
77
77
  <div>
78
78
  <label for="r1">グー:</label>
79
- <input type="radio" id="r1" name="janken" value1="勝">
79
+ <input type="radio" id="r1" name="janken" value="勝">
80
80
  </div>
81
81
  <div>
82
82
  <label for="r2">チョキ:</label>
83
- <input type="radio" id="r2" name="janken" value2="負け">
83
+ <input type="radio" id="r2" name="janken" value="負け">
84
84
  </div>
85
85
  <div>
86
86
  <label for="r3">パー:</label>
87
- <input type="radio" id="r3" name="janken" value3="引き分け">
87
+ <input type="radio" id="r3" name="janken" value="引き分け">
88
88
  </div>
89
89
  <div>
90
90
  <input type="submit" value="じゃんけんぽん!">

3

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

2020/03/19 00:02

投稿

reok
reok

スコア5

title CHANGED
File without changes
body CHANGED
@@ -9,7 +9,8 @@
9
9
 
10
10
  ```
11
11
  File "c:/Users/test/server.py", line 16, in post
12
- user_choice = radio[name]
12
+ user_choice = radio[janken]
13
+ KeyError: 'on'
13
14
  ```
14
15
 
15
16
  ### 該当のソースコード

2

ソースの修正

2020/03/18 10:13

投稿

reok
reok

スコア5

title CHANGED
File without changes
body CHANGED
@@ -21,19 +21,19 @@
21
21
  app = Flask(__name__)
22
22
  @app.route('/', methods=['GET'])
23
23
  def get():
24
- return render_template('index.html', \
24
+ return render_template('index.html', \
25
- title = 'Form Sample(get)', \
25
+ title = 'Form Sample(get)', \
26
- message = 'じゃんけんをはじめます')
26
+ message = 'じゃんけんをはじめます')
27
27
 
28
28
  @app.route('/', methods=['POST'])
29
29
  def post():
30
30
  radio = {"1": "グー","2": "チョキ","3": "パー"}
31
- name = request.form.get('radio')
31
+ janken = request.form.get('janken')
32
32
 
33
- user_choice = radio[name]
33
+ user_choice = radio[janken]
34
34
 
35
35
  choice_list = ["1", "'2", "3"]
36
- pc = name[random.choice(choice_list)]
36
+ pc = janken[random.choice(choice_list)]
37
37
 
38
38
  draw = '<font color="#32CD32">DRAW</font>'
39
39
  win = '<font color="#FF7F50">You Win!!</font>'
@@ -60,36 +60,35 @@
60
60
  else:
61
61
  judge = lose
62
62
 
63
- name = request.form.get('radio')
63
+
64
-
65
64
  return render_template('index.html',
66
- name=name, radio=radio)
65
+ radio=radio)
67
66
  if __name__ == '__main__':
68
- app.run()
67
+ app.run()
69
68
  ```
70
69
  ### 該当のソースコード
71
70
 
72
71
  ```
73
72
  <body>
74
- <h1>{{ title }}</h1>
73
+ <h1>{{ title }}</h1>
75
- <p>{{ message }}</p>
74
+ <p>{{ message }}</p>
76
- <form action="/" method="POST" enctype="multipart/form-data">
75
+ <form action="/" method="POST" enctype="multipart/form-data">
77
- <div>
76
+ <div>
78
- <label for="r1">グー:</label>
77
+ <label for="r1">グー:</label>
79
- <input type="radio" id="r1" name_1="radio1" value1="勝ち">
78
+ <input type="radio" id="r1" name="janken" value1="勝ち">
80
- </div>
79
+ </div>
81
- <div>
80
+ <div>
82
- <label for="r2">チョキ:</label>
81
+ <label for="r2">チョキ:</label>
83
- <input type="radio" id="r2" name_2="radio2" value2="負け">
82
+ <input type="radio" id="r2" name="janken" value2="負け">
84
- </div>
83
+ </div>
85
- <div>
84
+ <div>
86
- <label for="r3">パー:</label>
85
+ <label for="r3">パー:</label>
87
- <input type="radio" id="r3" name_3="radio3" value3="引き分け">
86
+ <input type="radio" id="r3" name="janken" value3="引き分け">
88
- </div>
87
+ </div>
89
- <div>
88
+ <div>
90
- <input type="submit" value="じゃんけんぽん!">
89
+ <input type="submit" value="じゃんけんぽん!">
91
- </div>
90
+ </div>
92
- </form>
91
+ </form>
93
92
  </body>
94
93
  ```
95
94
  ### 試したこと

1

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

2020/03/18 10:12

投稿

reok
reok

スコア5

title CHANGED
File without changes
body CHANGED
@@ -8,13 +8,13 @@
8
8
  ### 発生している問題・エラーメッセージ
9
9
 
10
10
  ```
11
- 気をつけた方がいい箇所が自力では見つけきれないので、出来るだけ頑張って書きました、添削をできればお願いします。
11
+ File "c:/Users/test/server.py", line 16, in post
12
+ user_choice = radio[name]
12
13
  ```
13
14
 
14
15
  ### 該当のソースコード
15
16
 
16
17
  ```
17
- @zyanken.py
18
18
  from flask import Flask, render_template, request
19
19
  import random
20
20
 
@@ -25,65 +25,66 @@
25
25
  title = 'Form Sample(get)', \
26
26
  message = 'じゃんけんをはじめます')
27
27
 
28
- @app.route('/zyanken', methods=['POST'])
28
+ @app.route('/', methods=['POST'])
29
29
  def post():
30
+ radio = {"1": "グー","2": "チョキ","3": "パー"}
31
+ name = request.form.get('radio')
30
32
 
33
+ user_choice = radio[name]
31
34
 
32
- radio = {"1": "グー","2": "チョキ","3": "パー"}
35
+ choice_list = ["1", "'2", "3"]
33
- name = request.form.get('radio')
36
+ pc = name[random.choice(choice_list)]
34
37
 
38
+ draw = '<font color="#32CD32">DRAW</font>'
39
+ win = '<font color="#FF7F50">You Win!!</font>'
35
- user_choice = radio[name]
40
+ lose = '<font color="#0000FF">You lose!!</font>'
36
41
 
37
- choice_list = ["1", "'2", "3"]
42
+ if user_choice == pc:
43
+ judge = draw
44
+ else:
38
- pc = name[random.choice(choice_list)]
45
+ if user_choice == "グー":
46
+ if pc == "チョキ":
47
+ judge = win
48
+ else:
49
+ judge = lose
39
50
 
40
- if user_choice == pc:
51
+ elif user_choice == "チョキ":
52
+ if pc == "パー":
41
- judge = radio_3
53
+ judge = win
42
- else:
54
+ else:
43
- if user_choice == "グー":
44
- if pc == "チョキ":
45
- judge = name_1
55
+ judge = lose
46
- else:
47
- judge = name_2
48
56
 
49
- elif user_choice == "チョキ":
50
- if pc == "パー":
51
- judge = name_1
52
57
  else:
58
+ if pc == "グー":
59
+ judge = win
60
+ else:
53
- judge = name_2
61
+ judge = lose
54
62
 
55
- else:
56
- if pc == "グー":
57
- judge = name_1
58
- else:
59
- judge = name_2
60
-
61
- name = request.form.get('radio' )
63
+ name = request.form.get('radio')
62
64
 
63
- return render_template('index.html',
65
+ return render_template('index.html',
64
- name=name, title=title)
66
+ name=name, radio=radio)
65
67
  if __name__ == '__main__':
66
68
  app.run()
67
69
  ```
68
70
  ### 該当のソースコード
69
71
 
70
72
  ```
71
- @index.html
72
73
  <body>
73
74
  <h1>{{ title }}</h1>
74
75
  <p>{{ message }}</p>
75
76
  <form action="/" method="POST" enctype="multipart/form-data">
76
77
  <div>
77
78
  <label for="r1">グー:</label>
78
- <input type="radio1" id="r1" name_1="radio1" value1="勝ち">
79
+ <input type="radio" id="r1" name_1="radio1" value1="勝ち">
79
80
  </div>
80
81
  <div>
81
82
  <label for="r2">チョキ:</label>
82
- <input type="radio2" id="r2" name_2="radio2" value2="負け">
83
+ <input type="radio" id="r2" name_2="radio2" value2="負け">
83
84
  </div>
84
- <div>
85
+ <div>
85
86
  <label for="r3">パー:</label>
86
- <input type="radio3" id="r3" name_3="radio3" value3="引き分け">
87
+ <input type="radio" id="r3" name_3="radio3" value3="引き分け">
87
88
  </div>
88
89
  <div>
89
90
  <input type="submit" value="じゃんけんぽん!">