Home > Net >  Quadratic Equation with Python and Flask
Quadratic Equation with Python and Flask

Time:02-11

So currently I am trying to make a program using flask, but I am getting a TypeError when I run it, saying The view function for 'post_factors' did not return a valid response. The function either returned None or ended without a return statement. and I cannot figure how to fix it. Here is the code:

import math

def dofactors(a, b, c):
    a = int(a)
    b = int(b)
    c = int(c)
    d = b**2-4*a*c # discriminant

    if d < 0:
        result = 'No solutions'
    elif d == 0:
        x1 = -b / (2*a)
        result = f'The sole solution is {str(x1)}'
    else: # if d > 0
        x1 = (-b   math.sqrt(d)) / (2*a)
        x2 = (-b - math.sqrt(d)) / (2*a)
        result = f'Solutions are {str(x1)} and {str(x2)}'

    return(result)

and the HTML:

<!DOCTYPE html>
<form method="POST">
  <p><h1>Factorizer</h1></p>
    <p>please enter your variable values assuming the form ax^2 bx c</p>
  <input type="text", placeholder="a", name="a" oninput="this.value = this.value.replace(/[^0-9.-]/g, '').replace(/(\..*)\./g, '$1');" />
  <input type="text", placeholder="b", name="b" oninput="this.value = this.value.replace(/[^0-9.-]/g, '').replace(/(\..*)\./g, '$1');" />
  <input type="text", placeholder="c", name="c" oninput="this.value = this.value.replace(/[^0-9.-]/g, '').replace(/(\..*)\./g, '$1');" />
  <input type="submit">
</form>

EDIT: I forgot to include the code that is calling it in the main class, here it is:

@app.route('/factors')
def form_factors():
    return render_template('factors.html')

and the POST function:

@app.route('/factors', methods=['POST'])
def post_factors():
    a = str(request.form['a'])
    b = str(request.form['b'])
    c = str(request.form['c'])
    dofactors(a, b, c)

CodePudding user response:

You are returning values in your dofactor function, but these 'return's don't apply to the route as they are in a different scope. You could fix this with the following in your post_factors route.

return(dofactors(a, b, c))
  • Related