类型错误:无法将序列乘以“float”类型的非 int | 烧瓶

我开始学习 Flask 是为了一个大约一个月的小项目。我想做一个计算身体参数(如 BMI 或 LBM)的应用程序。问题是,当我请求表单中的数据时,它以元组的形式出现,因此 body_calculator 模块无法使用它,并在标题中抛出错误。我的问题是:为什么数据以元组的形式出现,在这种情况下,在 Flask 中请求数据的正确方法是什么?


烧瓶代码


from flask import Flask, url_for, render_template, make_response, request, redirect, session

import body_calculator  



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


 

@app.route('/', methods = ['GET','POST'])



def index():

       

    if request.method == 'POST': 

        


        height = int(request.form['height']),  

        weight = int(request.form['weight']), 

        age = int(request.form['age']), 

        sex = bool(request.form['sex']), 

        waist = int(request.form['waist'])



        body = body_calculator.Parameters(height, weight, age, sex, waist)


        LBM = body.Lean_Body_Mass()

        BMR = body.Basal_Metabolic_Rate()

        BFP = body.Body_Fat_Percentage()

        BMI = body.Body_Mass_Index()


            

        context = {

            'height' : height, 

            'weight' : weight, 

            'age' : age, 

            'sex': sex, 

            'waist' : waist,

            'LBM' : LBM,

            'BMR' : BMR,

            'BMI' : BMI,

            'BFP' : BFP

            }


        return render_template('index.html', **context)   

          

    else: 

        return render_template('index.html')



if __name__ == "__main__":

    app.run(debug=True)

body_calculator模块


    

BMI = None


class Parameters:



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

        self.height = height

        self.weight = weight

        self.age = age

        self.sex = sex

        self.waist = waist

        

    

    # Body Lean Mass function

    def Lean_Body_Mass(self):

        if self.sex == 0: 

            BLM = (0.3281 * self.weight) + (0.33929 * self.height) - 29.5336

            return round(BLM,2) 


ITMISS
浏览 78回答 1
1回答

慕村9548890

这几行看起来有问题:height = int(request.form['height']),   weight = int(request.form['weight']),  age = int(request.form['age']),  sex = bool(request.form['sex']),请注意,您编写了一个尾随逗号,它将值放入length-1 元组中:因此,例如第一行height从表单中获取,将其转换为 an int,然后将其放入 length-1 元组中。尝试删除那些结尾的逗号。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python