-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.py
55 lines (45 loc) · 1.77 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
import random
from flask import Flask, render_template, flash, redirect, url_for, session
from flask_wtf import FlaskForm
from wtforms import IntegerField, SubmitField
from wtforms.validators import DataRequired, NumberRange
from flask_bootstrap import Bootstrap
app = Flask(__name__)
app.config['SECRET_KEY'] = 'very hard to guess string'
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
bootstrap = Bootstrap(app)
class GuessNumberForm(FlaskForm):
number = IntegerField(u'输入一个整数(0~1000)', validators=[
DataRequired(u'请输入一个有效的整数!'),
NumberRange(0, 1000, u'请输入0~1000以内的整数!')])
submit = SubmitField(u'提交')
@app.route('/')
def index():
# generate a random number in 0~1000, store it into session.
session['number'] = random.randint(0, 1000)
session['times'] = 10
return render_template('index.html')
@app.route('/guess', methods=['GET', 'POST'])
def guess():
times = session['times']
result = session['number']
form = GuessNumberForm()
if form.validate_on_submit():
times -= 1
session['times'] = times # update session value
if times < 0:
flash(u'你输啦……o(>﹏<)o', 'danger')
return redirect(url_for('index'))
answer = form.number.data
if answer > result:
flash(u'太大了!你还剩下%s次机会。' % times, 'warning')
elif answer < result:
flash(u'太小了!你还剩下%s次机会。' % times, 'info')
else:
flash(u'啊哈,你赢了!V(^-^)V', 'success')
return redirect(url_for('index'))
return redirect(url_for('guess'))
return render_template('guess.html', form=form)
if __name__ == '__main__':
app.run()