烧瓶重置全球词典
问题描述:
我在烧瓶中制作一个tic tac toe用于学习目的。董事会被表示为字典,是一个全局变量。我想有一个“复位”板的按钮,使其可以再次播放,但是我的代码不起作用重置哪个执行,但板值不会改变。烧瓶重置全球词典
任何想法我做错了什么? 非常感谢!
theBoard = {1:' ', 2:' ', 3:' ', 4: ' ', 5:' ', 6: ' ', 7:' ', 8:' ', 9:' '}
@app.route('/reset', methods=["GET", "POST"])
def reset():
for i in range (1,9):
theBoard[i] == ' '
return render_template("test.html", theBoard=theBoard)
@app.route('/play', methods=["GET","POST"])
def test1():
return render_template("test.html", theBoard=theBoard)
@app.route('/play1', methods=["GET", "POST"])
def test():
if gameover(theBoard):
True
return 'the game is over1'
else:
x = request.form['move']
move = int(x)
valid_moves = [1,2,3,4,5,6,7,8,9]
if move not in valid_moves:
return 'you did not specify a valid move, please try again!'
elif theBoard[move] != ' ':
return 'you can not play that space, it is taken'
else:
theBoard[move] = 'X'
if gameover(theBoard):
True
return 'the game is over2'
if winning_X(theBoard):
<and much more code - this part works>
在HTML:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<div class="enter name">
<form action="/play1" method="POST">
<lable>Please specify your move (1,2,3,4,5,6,7,8,9)</lable>
<input type="number" name="move" value"">
<input type="submit" value="Make your move!">
</form>
</div>
<div>
<table border="1">
<tr id="row1">
{% if theBoard[1]!=' ' %}
<td><h1>{{ theBoard[1] }} </h1></td>
{% else %}
<td><h1> 1 </h1></td>
{% endif %}
{% if theBoard[2]!=' ' %}
<td><h1>{{ theBoard[2] }} </h1></td>
{% else %}
<td><h1> 2 </h1></td>
{% endif %}
{% if theBoard[3]!=' ' %}
<td><h1>{{ theBoard[3] }} </h1></td>
{% else %}
<td><h1> 3 </h1></td>
{% endif %}
<tr id="row2">
{% if theBoard[4]!=' ' %}
<td><h1>{{ theBoard[4] }} </h1></td>
{% else %}
<td><h1> 4 </h1></td>
{% endif %}
{% if theBoard[5]!=' ' %}
<td><h1>{{ theBoard[5] }} </h1></td>
{% else %}
<td><h1> 5 </h1></td>
{% endif %}
{% if theBoard[6]!=' ' %}
<td><h1>{{ theBoard[6] }} </h1></td>
{% else %}
<td><h1> 6 </h1></td>
{% endif %}
<tr id="row3">
{% if theBoard[7]!=' ' %}
<td><h1>{{ theBoard[7] }} </h1></td>
{% else %}
<td><h1> 7 </h1></td>
{% endif %}
{% if theBoard[8]!=' ' %}
<td><h1>{{ theBoard[8] }} </h1></td>
{% else %}
<td><h1> 8 </h1></td>
{% endif %}
{% if theBoard[9]!=' ' %}
<td><h1>{{ theBoard[9] }} </h1></td>
{% else %}
<td><h1> 9 </h1></td>
{% endif %}
</table>
</div>
<div class="reset">
<form action="/reset" method="GET">
<lable>Do you wanna play again?</lable>
<button>Play!</button>
</form>
</div>
</body>
</html>
{% endblock %}
答
下面的代码是导致从我可以看到的错误:
theBoard[i] == ' '
以上实际上执行的比较不是将其更改为:
theBoard[i] = ' '
+0
对不起还没有看到你的答案,一会儿回来! –
答
HTML按钮调用其上执行/复位,但是板的值不改变。
def reset():
for i in range (1,9):
theBoard[i] == ' ' # <--- This line
return render_template("test.html", theBoard=theBoard)
您使用==
这是比较返回要么True
或False
,你想要什么=
运算符(赋值运算符,一个等号),所以:theBoard[i] = ' '
您还可以分享您用于发布主板的标记的相关代码片段吗? – joemurphy
@joemurphy我认为你的意思是HTML? (抱歉,我仍在学习)。上面加了 –