Skip to main content
 首页 » 编程设计

python之值错误 : invalid literal for int() with base 10: '' Unable to spot error

2025年05月04日6yyy_WW

我的 view.py 如下所示。我认为错误在这一部分。如果现在这还不够,我会发布一个详细的功能。基本上以总计为参数的模型是整数类型。

编辑 1:编写整个函数,以便更有帮助。

def subjManip(request): 
 
    c= {} 
    c.update(csrf(request)) 
    subj = None 
    c.update(csrf(request)) 
    if request.method == 'POST': 
        form = SubjForm(request.POST) 
 
        if form.is_valid(): 
               user = request.user 
               subj1 = form.cleaned_data['subj1'] 
               subj2 = form.cleaned_data['subj2'] 
               subj3 = form.cleaned_data['subj3'] 
 
               if subj1 == None and subj2 == None: 
                   raise forms.ValidationError("Both subjs cannot be empty") 
 
 
               tot = float (((float(0.2017 * subj1 )+ float (0.09036 * subj2) + float(0.6309 * subj3) - 5.0269)/ 4.184) 
               total =int(tot) 
 
               elif subj1 == None: 
                   total = subj1 

请您参考如下方法:

尝试将 tot = (subj1+subj2)/2.2 替换为 tot = (float(subj1) + float(subj2))/2.2