繁体   English   中英

在for循环中为变量赋值

[英]Assigning value to variable inside for loop

我有一个 for 循环,它遍历城市列表以及它们的广泛变量(安全性、可负担性、交通等)。 如果城市的广泛和特定变量与用户在页面上选择的广泛和特定变量之间存在匹配,那么它会分配一个加权值,否则值为 0。我正在尝试为列表中的每个广泛变量添加值,但我得到了错误

赋值前引用的局部变量“city_variable1_value”

当我在第一个 if 语句之前将 city_variable1_value 和 city_variable2_value 引用为 0 时,我

total_city_value = city_variable1_value + city_variable2_value

例如,当它应该是 0.24 时等于 0。 代码如下!

视图.py

# list of each variable's broad category
broad_variable_list = ["safety", "affordability", "transit", "language", "attractions"]

# weighting of each variable ranking
weight_variable1 =  0.33
weight_variable2 =  0.24
weight_variable3 =  0.17
weight_variable4 =  0.14
weight_variable5 =  0.12

def get_ranking(request):
    
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        
        # create a form instance and populate it with data from the request:
        form = RankingForm(request.POST)
        
        # check whether it's valid:
        if form.is_valid():
            
            # process the data in form.cleaned_data as required
            specific_variable1 = form.cleaned_data['specific_variable1']
            specific_variable2 = form.cleaned_data['specific_variable2']

            broad_variable1 = form.cleaned_data['broad_variable1']
            broad_variable2 = form.cleaned_data['broad_variable2']

            # loop through each city in the database
            for cities in City.objects.values_list('city', flat=True):
                print(cities)

                # loop through each broad variable in the list at the top of this page
                for broad_variable in broad_variable_list:

                    # check if the current broad variable is equal to the first broad variable the user chose
                    if broad_variable == broad_variable1:
                        
                        # if the city's specific variable is equal to the specific variable the user chose get the value of it based on it's ranking weight
                        if City.objects.values_list(broad_variable, flat=True).filter(city=cities).first() == specific_variable1:
                            city_variable1_value = 1 * weight_variable1
                            print(city_variable1_value)
                        
                        # else assign a value of 0
                        else:
                            city_variable1_value = 0
                            print(city_variable1_value)

                    # check if the current broad variable is equal to the second broad variable the user chose
                    if broad_variable == broad_variable2:
                        
                        # if the city's specific variable is equal to the specific variable the user chose get the value of it based on it's ranking weight
                        if City.objects.values_list(broad_variable, flat=True).filter(city=cities).first() == specific_variable2:
                            city_variable2_value = 1 * weight_variable2
                            print(city_variable2_value)
                        
                        # else assign a value of 0
                        else:
                            city_variable2_value = 0
                            print(city_variable2_value)
                    
                    total_city_value = city_variable1_value + city_variable2_value
                    print(total_city_value)
                    
        # if a GET (or any other method) we'll create a blank form
    else:
        form = RankingForm()

    return render(request, 'project/home.html', {'form': form})

您的程序达到了total_city_value = city_variable1_value + city_variable2_value行,而从未为city_variable1_value分配值。 如果条件broad_variable == broad_variable1在分配给city_variable1_value的 for 循环中从未为真,这是可能的。

举个简单的例子,如果你这样做,

def f():
    if False:
        a = 10
    print(a)
f()

你会得到同样的错误UnboundLocalError: local variable 'a' referenced before assignment

要解决此问题,只需在 if 语句之前为变量分配一个默认值,这可能会阻止分配发生。 例如,

city_variable1_value = 0
if broad_variable == broad_variable1:
    ...

city_variable2_value = 0
if broad_variable == broad_variable2:
    ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM