FLASK和WTForms | TypeError:*:'NoneType'和'int'不受支持的操作数类型

问题描述

我正在尝试学习Flask来创建一个应用程序,该应用程序会进行一些身体测量并将身体参数返回为“身体质量指数”或“身体脂肪百分比”。问题是我正在使用wtform创建表单,并且当我尝试使用它时,数据以nonetype的形式出现并抛出此错误

TypeError: unsupported operand type(s) for *: 'nonetype' and 'int'

烧瓶代码


from flask import Flask,url_for,render_template,make_response,request,redirect,session
from forms import DecimalField,BooleanField 
import body_calculator  



app = Flask(__name__,static_folder='static',template_folder='templates')

@app.route('/',methods = ['GET','POST'])
def index():
    
    form = DecimalField()
    
    sex = request.form.get('sex')
    age = request.form.get('age')
    height = request.form.get('height')
    weight = request.form.get('weight')
    waist = request.form.get('waist')
    
    body = body_calculator.Parameters(height,weight,age,sex,waist)

    LBM = body.Body_Lean_Mass()
    BMR = body.Basal_Metabolic_Rate()
    BFP = body.Body_Fat_Percentage()
    BMI = body.Body_Mass_Index()

        
    context = {
        'int_form' : form,'LBM' : LBM,'BMR' : BMR,'BMI' : BMI
        }
    
    if request.method == 'POST' and form.validate():
        return render_template('index.html',**context)


if __name__ == "__main__":
    app.run(debug=True) 

计算器代码

BMI = None

class Parameters:

    def __init__(self,height,waist):
        self.height = height
        self.weight = weight
        self.age = age
        self.sex = sex
        self.waist = waist
    

# Body Lean Mass function
def Body_Lean_Mass(self):
    if self.sex == 0: 
        BLM = (0.3281 * self.weight) + (0.33929 * self.height) - 29.5336
        return round(BLM,2) 
    
    if self.sex == 1:
        BLM = (0.29569 * self.weight) + (0.41813 * self.height) - 43.2933
        return round(BLM,2) 
    
# Body Mass Index function
def Body_Mass_Index(self):
    
    global BMI
    BMI = self.weight / (self.height / 100) **2 
    return round(BMI,2)


# Body Fat Percentage 
def Body_Fat_Percentage(self):
    
    if self.sex == 0:
        BFP = 1.20 * BMI + 0.23 * self.age - 16.2
        if self.age <= 15:
            BFP = 1.51 * BMI - 0.70 * self.age - 2.2
        return round(BFP,2) 
    
    
    if self.sex == 1:
        BFP = 1.20 * BMI + 0.23 * self.age - 5.4
        if self.age <= 15:
            BFP = 1.51 * BMI - 0.70 * self.age + 1.4
        return round(BFP,2) 

    
# Basal Metabolic Rate
def Basal_Metabolic_Rate (self): 
    s = 0
    
    if self.sex == 0:
        s =+ 5
    if self.sex == 1:
        s =- 161
    
    BMR = (self.weight * 10 ) + (self.height * 6.25) - (self.age * 5) + s 
    return round(BMR,2) 
``

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)