flask UnboundLocalError:分配前引用的局部变量'shift1'
问题描述:
********************* ********************* *********************我试图让使用烧瓶的公告板,代码工作完美之前但突然停止工作。请help.I没有与烧瓶太多的经验,我也试着声明变量全球flask UnboundLocalError:分配前引用的局部变量'shift1'
的Python(瓶)代码:
from flask import Flask, render_template
import datetime
import MySQLdb
app = Flask(__name__)
@app.route('/shutdown', methods=['POST'])
def shutdown():
shutdown_server()
return 'Server shutting down...'
@app.route('/')
def index():
now = datetime.datetime.now()
timestamp = datetime.datetime.now().time()
if datetime.time(7, 30) <= timestamp <= datetime.time(15, 29):
shift1 = "A"
elif datetime.time(15, 30) <= timestamp <= datetime.time(23, 29):
shift1 = "B"
elif datetime.time(23, 30) <= timestamp <= datetime.time(7, 29):
shift1 = "C"
return render_template('hello.html', month=now.month, date=now.day, year=now.year, hour=now.hour, minute=now.minute, second=now.second, shift=shift1)
if __name__ == '__main__':
app.run(host= '0.0.0.0', debug=True)
HTML代码:
<html>
<head>
<script>
setInterval(function() {
window.location.reload();
}, 1000);
</script>
<style>
table, td, tr {
text-align: center;
height: 72px;
font-size: 53;
}
</style>
</head>
<body>
<table border="1" width="100%" height="941px">
<tr>
<td colspan="4"><p align="centre"><font color="blue">{{ date }}/{{ month }}/{{ year }}</font></p></td>
<td colspan="4"><p align="centre"><font color="blue">{{ hour }} : {{ minute }} : {{ second }}</font></p></td>
</tr>
<tr>
<td colspan="5"></td>
<td colspan="2" ><font>Shift:</td>
<td colspan="1" ><font color="red">{{ shift }}</td>
</tr>
<tr>
<td colspan="2" width="20%"><font size="28" color="green">Plan</td>
<td width="10%">1</td>
<td></td>
<td rowspan="8" colspan="4"></td>
</tr>
<tr>
<td colspan="2" rowspan="3"></td>
<td width="10%">2</td>
<td></td>
</tr>
<tr>
<td width="10%">3</td>
<td></td>
</tr>
<tr>
<td width="10%">4</td>
<td></td>
</tr>
<tr>
<td colspan="2"height="50px" width="20%"><font size="28" color="green">Actual</td>
<td width="10%">5</td>
<td></td>
</tr>
<td colspan="2" rowspan="3"></td>
<td width="10%">6</td>
<td></td>
</tr>
<tr>
<td width="10%">7</td>
<td></td>
</tr>
<tr>
<td width="10%">8</td>
<td></td>
</tr>
</table>
</body>
</html>
错误:
答
你的时间比较是有缺陷的;您正在无视一个time()
对象的秒部分:
>>> import datetime
>>> datetime.time(15, 29, 30) <= datetime.time(15, 29)
False
>>> datetime.time(15, 29, 30) >= datetime.time(15, 30)
False
秒部分默认为0
,所以15:29:30
是后比15:29:00
。这意味着有时间戳不会导致任何if
.. elif
分支匹配,并且shift1
从不设置。
使用<
测试代替,并用更好的上边界:
if datetime.time(7, 30) < timestamp < datetime.time(15, 30):
shift1 = "A"
elif datetime.time(15, 30) < timestamp < datetime.time(23, 30):
shift1 = "B"
elif datetime.time(23, 30) < timestamp < datetime.time(7, 30):
shift1 = "C"
对于转移的数量较多,我会使用二分法;设置的开始时间在一个序列,然后使用所得到的插入点的当前时间作为一个指数到移位的序列:
from bisect import bisect
shift_starts = (datetime.time(7, 30), datetime.time(15, 30), datetime.time(23, 30))
shift_names = 'CABC' # C twice to account for the time before 7:30
shift_index = bisect(shift_starts, timestamp)
shift1 = shift_names[shift_index]
请包括在问题作为代码块的所有错误 –