我有一個 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})
uj5u.com熱心網友回復:
您的程式到達了該total_city_value = city_variable1_value city_variable2_value行而從未為 賦值city_variable1_value。如果在broad_variable == broad_variable1分配給 的 for 回圈中條件從不為真,則這是可能的city_variable1_value。
舉個最簡單的例子,如果你這樣做了,
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:
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/384794.html
上一篇:JavaScript:使If陳述句在添加變數后調整為變數
下一篇:試圖制作一種二十一點游戲
