[英]If the value recieved in a Decimal Form is an empty string how can I convert it to a float number?
On my page I ask for various things to the user in the form as it's a calculator that does various things, now you see, the thing is if the user does not have the value for bf I want to give the option to calculate with different values, that is why I required is False, so first what I want to archive is that if the form is empty I want to be passed as a 0 or False, how can I archive this?在我的页面上,我以表格形式向用户询问各种事情,因为它是一个可以做各种事情的计算器,现在你看到了,问题是如果用户没有 bf 的值,我想提供使用不同计算的选项值,这就是为什么我需要的是 False,所以首先我要归档的是,如果表单为空,我想作为 0 或 False 传递,我该如何归档呢?
I have the following form:我有以下表格:
class CMForm(forms.Form):
age = forms.DecimalField(max_digits=2, decimal_places=0, min_value=15, max_value=80)
sex = forms.ChoiceField(choices=sex)
pregnant = forms.BooleanField(initial=False, required=False)
lactating = forms.BooleanField(initial=False, required=False)
weight = forms.DecimalField(max_digits=4, decimal_places=1, max_value=635)
height = forms.DecimalField(max_digits=4, decimal_places=1, max_value=272)
bf = forms.DecimalField(label='Body Fat(not required)', help_text='%', required=False, decimal_places=1, min_value=1, max_value=80)
activeness = forms.ChoiceField(choices=activity_level)
units = forms.ChoiceField(choices=units)
this view:这种观点:
def cmr(request):
a = float(request.GET['age'])
bf = float(request.GET.get('bf', 0))
s = str(request.GET['sex'])
g = 0
if s == 'female':
g = 1
w = float(request.GET['weight'])
h = float(request.GET['height'])
act = str(request.GET['activeness'])
u = str(request.GET['units'])
p = str(request.GET.get('pregnant', 0))
lac = str(request.GET.get('lactating', 0))
dci = 0
bmrm = 10*w+6.25*h-5*a+5
bmrf = 10*w+6.25*h-5*a-161
bmi = round(w/(h/100)**2, 1)
if bf is False:
bf = round(-44.988 + (0.503 * a) + (10.689 * g) + (3.172 * bmi) - (0.026 * bmi**2) + (0.181 * bmi * g) - (0.02 * bmi * a) - (0.005 * bmi**2 * g) + (0.00021 * bmi**2 * a), 1)
tbf = w*(bf/100)
ffm = w*(1-(bf/100))
ffmi = ffm/(h/100)**2
nffmi = ffmi+6.1*(1.8-(h/100))
if p == 'on':
dci = 300
if lac == 'on':
dci = dci+500
if s == 'male':
bmr = dci+bmrm
dcis = bmrm*1.2
dcil = bmrm*1.375
dcim = bmrm*1.55
dciv = bmrm*1.725
dcie = bmrm*1.9
else:
bmr = dci+bmrf
dcis = dci+bmrf*1.2
dcil = dci+bmrf*1.375
dcim = dci+bmrf*1.55
dciv = dci+bmrf*1.725
dcie = dci+bmrf*1.9
context = {'dci': round(int(dci)),
'p': p,
'lac': lac,
'bmr': int(bmr),
'dcis': int(dcis),
'dcil': int(dcil),
'dcim': int(dcim),
'dciv': int(dciv),
'dcie': int(dcie),
'act': act,
'bmi': bmi,
'bf': bf,
'tbf': round(tbf, 1),
'ffm': round(ffm, 1),
'ffmi': round(ffmi, 1),
'nffmi': round(nffmi, 1),
}
return render(request, "calorie_maintenance_result.html", context)
And I get the following error:我收到以下错误:
could not convert string to float: ''
You can use or
;您可以使用or
; given a string s
, s or 0
will evaluate to 0
if s
is empty:给定字符串s
,如果s
为空,则s or 0
将计算为0
:
s = ''
print(float(s or 0)) # 0.0
s = '123'
print(float(s or 0)) # 123.0
This happens because (in python) x or y
returns x
if x
is true, and y
if x
is false.发生这种情况是因为(在 python 中) x or y
如果x
为真则返回x
,如果x
为假则返回y
。 Since an empty string is "falsy", '' or 0
is equal to 0
.由于空字符串是“falsy”,因此'' or 0
等于0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.