File size: 1,326 Bytes
16a6309 b377933 |
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 |
from flask import Flask, render_template, request
from sympy import symbols, Eq, solve
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
solutions = []
error = None # 初始化错误消息
if request.method == 'POST':
equation = request.form['equation']
result = solve_equation_helper(equation)
if 'error' in result: # 如果返回的是错误消息字典
error = result['error'] # 设置错误消息
else:
solutions = result # 设置解方程结果
return render_template('yyfc.html', solutions=solutions,
error=error) # 传入错误消息参数
def solve_equation_helper(equation):
try:
# 将等式按等号分割成左右两部分
left, right = equation.split('=')
# 创建符号变量
x = symbols('x')
# 创建方程
eq = Eq(eval(left), eval(right))
# 求解方程
solutions = solve(eq, x)
return solutions
except Exception:
error_message = "请确保未知数为x,并且输入正确,其中*表示乘号,**表示乘方,/表示分式。如果还有问题,请把错误提示和发生错误的时间报告给星空暗夜团队。"
return {'error': error_message} # 返回带有错误消息的字典
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)
|