TypeError:无法将序列与“float”类型的非整数相乘

2024-04-26 13:55:41 发布

您现在位置:Python中文网/ 问答频道 /正文

I started to learn Flask for a mini-project about a month. I wanted to do an app that calculates body parameters as BMI or LBM. The thing is that when I request the data in the forms, it come as tuples, so it can't be used by the body_calculator module, and throws the error in the title. My questions are: why data come as tuple, and which is the correct way to request data in Flask in this situations?

烧瓶代码

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)

车身_计算器模块

    
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) 
        
        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)  

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Workout calculator</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <form action="/" method = "POST">
    <div class="wrapper">

        <div class="title">
            Workout calculator
        </div>

        <div class="form">
            <div class="gender"> 
                <label for="men">Men</label>
                <input type="checkbox" id=men name='sex' value=0/>
                <label for="women">Women</label>
                <input type="checkbox" id=women name='sex' value=1/>
            </div>
            

                <div class="input_field">
                    <label for="height">Height</label>
                    <input type="number" id="height" name="height"/>
                    <select id="height">
                        <option value="cm">cm</option>
                        <option value="ft">ft</option>
                    </select>
                </div>
           
            <div class="input_field">
                <label for="weight">Weight</label>
                <input type="number" id="weight" name="weight" />
                <select id="weight">
                    <option value="kg">kg</option>
                    <option value="lbs">lbs</option>
                </select>
            </div>

            <div class="input_field">
                  <label for="age">Age</label>
                  <input type="number" id=age name="age" />
            </div>

            <div class="input_field">
                <label for="waist">Waist</label>
                <input type="number" id=waist name="waist" />
                <select id="waist">
                    <option value="cm">cm</option>
                    <option value="ft">ft</option>
                </select>
            </div>

        </div>
        <div class="input_field">
            <input type="submit" class="calculate" id="calculate" />
        </div>
    </div>
    </form>

    <body>
        <div class="results">
            <div class="BMI">
                <p>BMI (Body Mass Index): {{ BMI }}</p>
            </div>
            <div class="Body fat">
                <p>BFP (Body Fat Percentage): {{ BFP }}</p>
            </div>
            <div class="Lean body mass">
                <p>LBM (Lean body mass): {{ LBM }}</p>
            </div>
            <div class="Waist / height ratio">
                <p>BMR (Basal Metabolic Rate): {{ BMR }}</p>
            </div>
        </div>
    </body>

</body>
</html>

Tags: selfdivinputagebodylabelclassoption
1条回答
网友
1楼 · 发布于 2024-04-26 13:55:41

这几行看起来像问题所在:

height = int(request.form['height']),  
weight = int(request.form['weight']), 
age = int(request.form['age']), 
sex = bool(request.form['sex']), 

注意,您编写了一个尾随逗号,它将值放入长度为1的元组:例如,第一行从表单中获取height,将其转换为int,并将其放入长度为1的元组。尝试删除那些尾随的逗号

相关:Unintentional trailing comma that creates a tuple

进一步阅读:

相关问题 更多 >