【发布时间】:2016-01-18 06:43:17
【问题描述】:
这是我几周前第一次学习python时编写的一个程序,它简单地求解二次公式,检查解是否无关,并找到二次图的一些关键特征,包括顶点、对称线、我什至得到它来考虑激进分子。这一切都很好,但它只在控制台中工作。
当我开始将它带到烧瓶应用程序并对其进行修改以接受用户输入时,它只能处理完美的数字而不是小数。如A=1 B=4 C=4。每当输入 A=2 b=1 C=4 之类的内容时,它都会给我:HTTP 405 错误。
main.py:
from flask import Flask, render_template, request
import math
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def quadratic():
if request.method == 'POST':
a = float(request.form['a'])
b = float(request.form['b'])
c = float(request.form['c'])
outside = b * -1
bsquared = b ** 2
four_a_c = 4 * a * c
discriminant = bsquared - four_a_c
bottom = 2 * a
discriminant_sqrt = math.sqrt(discriminant)
top = outside + discriminant_sqrt
top2 = outside - discriminant_sqrt
root = top/bottom
root2 = top2/bottom
equation = a * root ** 2 + b * root + c
equation2 = a * root2 ** 2 + b * root + c
if equation < 1 and equation > -1:
Ex = "Not Extraneous"
else:
Ex = "Extraneous"
if equation2 < 1 and equation2 > -1:
Ex2 = "Not Extraneous"
else:
Ex2 = "Extraneous"
return render_template('form.html', discriminant=discriminant, a=a, b=b, c=c, outside=outside, bsquared=bsquared, bottom=bottom, root=root, root2=root2, ex=Ex, ex2=Ex2)
if request.method == 'GET':
return render_template('form.html')
if __name__ == '__main__':
app.run()
form.html:
<html>
<body>
<form method="POST" action=".">
A <input id="post_form_id" name="a" value="" />
B<input id ="post_form_id" name="b" value="" />
C <input id ="post_form_id" name="c" value="" />
<input type="submit" />
</form>
<br />
{% if a %}
A: {{ a }} <br />
B: {{ b }} <br />
C: {{ c }} <br />
Roots: <br />
{{ outside }} + √{{ discriminant }} <br/>
--------- <br/>
{{ bottom }}<br/>
{{ outside }} - √{{ discriminant }} <br/>
--------- <br/>
{{ bottom }}<br/>
Approxomated Roots: <br/>
{{ root }} <br/>
{{ ex }} <br/>
{{ root2}} <br/>
{{ ex2 }} <br/>
{% endif %}
</body>
</html>
【问题讨论】:
-
请不要竖起不必要的代码墙。请参阅帮助中的this section。
标签: python html python-3.x flask