python - TypeError:不能将序列乘以“浮点”类型的非整数 | 烧瓶
问题描述
我开始为一个小项目学习 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)
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>
解决方案
这几行看起来像问题:
height = int(request.form['height']),
weight = int(request.form['weight']),
age = int(request.form['age']),
sex = bool(request.form['sex']),
请注意,您编写了一个尾随逗号,它将值放入长度为 1 的元组中:例如,第一行height
取自表单,将其转换为int
,并将其放入长度为 1 的元组中。尝试删除那些尾随逗号。
相关:创建元组的无意尾随逗号
进一步阅读:
推荐阅读
- android - 在 Kotlin 中使用 safeArgs 导航抛出和错误
- android - 在 Android Studio 中将 SQL 数据导出到 Excel
- git - git diff 忽略空格,除非删除空格
- c - 如何使用 UEFI 运行时服务?
- python - django-background-tasks 管理命令未在 AWS elasticbeanstalk 上运行
- flutter - 每次按下按钮时,如何将 ListTile 添加到列表中?
- ansible - 如何访问另一台服务器的 ansible 事实?
- bash - 用于从串行设备读取 N 字节二进制数据并将其保存到文件的 Bash 脚本
- bash - If/Elif 结构有什么问题吗?
- numpy - 我需要解决导入 NumPy 库的错误