简体   繁体   English

为什么会出现错误“赋值之前引用本地变量”或“未定义全局变量”的错误?

[英]Why am I getting either error “local variable referenced before assignment” or “global variable not defined”?

First of all, apologies for the very long, very terrible code below. 首先,为下面很长很糟糕的代码道歉。 It is work in progress and I pasted all of it in because it will probably need to be run in order to find where the problem is. 它正在进行中,我将其全部粘贴了,因为可能需要运行它才能找到问题所在。

I am getting the error code "local variable 'dennisov_type' referenced before assignment" and I assume it is because I am trying to reference it from an outer level (outside of the indent). 我收到错误代码“分配前已引用本地变量'dennisov_type'”,我认为这是因为我试图从外部(缩进之外)引用它。 However I have tried to make the variable that is to be referenced (dennisov_type) a global variable by typing "global dennisov_type" on the line before the if statements, as well as after, inside the the function, outside of it, pretty much everywhere I could. 但是,我试图通过在if语句之前以及之后,在函数内部,函数外部,几乎在所有地方的行上键入“ global dennisov_type”,使要引用的变量(dennisov_type)成为全局变量。我可以。 But I am then getting the error code "global name 'dennisov_type' is not defined" when I do that, even though it IS defined. 但是当我这样做时,即使得到了定义,我也得到了错误代码“未定义全局名称'dennisov_type'”。

The code appears after an option is selected from the dropdown box in the Tkinter window and the 'select' button is pressed. 从Tkinter窗口的下拉框中选择一个选项并按下“选择”按钮后,将显示代码。

Would someone mind running this for me in a Python editor and letting me know where I have gone wrong please? 有人介意在Python编辑器中为我运行此操作,并告诉我哪里出了问题吗?

from Tkinter import Tk, Button, Canvas, END, Spinbox
from ttk import Progressbar, Combobox
from urllib import urlopen
from re import findall
import re
import itertools

## Create a window
window = Tk()

## Give the window a title
window.title('Watch finder')

## Types of Dennisov watches
dennisov_type_list = ['Barracuda Limited','Barracuda Chronograph',
                      'Barracuda Mechanical','Speedster','Free Rider',
                      'Nau Automatic','Lady Flower','Enigma','Number One']

dennisov_file = open('dennisov_url.html', 'w')

dennisov_file.write('''
<!DOCTYPE html>
<html>
    <head>
        <title>Watches</title>
    </head>
    <body>
''')

#### Display the quanity of watches available for each type
##quantity_box = len(watch_option)
##
#### 
##for watch, number in enumerate(watch_option):
##    watch_number_name = watch_option[number][0]
##    watch_number_image = watch_option[number][1]
##    watch_number_price = watch_option[number][2]
##    watch_number_link = watch_option[(number)][3]
####    dennisov_file.write('<h1>' + watch_number_name + '</h1>')
##    print watch_number_image
####    dennisov_file.write('<img src="' + watch_number_image + '">\n')
##    print watch_number_price
####    dennisov_file.write('<h2>' + watch_number_price + '</h2>')
##    print watch_number_link
####    dennisov_file.write('<a href="'+watch_number_link+'">'+watch_number_link+'</a>')
##       

dennisov_file.write('''
</body>
</html>
''')

## Define function for ...............
def display_choice():
    dennisov_type_selection = (dennisov_type_dropdown.get() + '\n')
    if dennisov_type_selection == 'Barracuda Limited':
        dennisov_type = 'barracuda_chronograph'
    elif dennisov_type_selection == 'Barracuda Chronograph':
        dennisov_type = 'barracuda_chronograph'
    elif dennisov_type_selection == 'Barracuda Mechanical':
        dennisov_type = 'barracuda_mechanical'
    elif dennisov_type_selection == 'Speedster':
        dennisov_type = 'speedster'
    elif dennisov_type_selection == 'Free Rider':
        dennisov_type = 'free_rider'
    elif dennisov_type_selection == 'Nau Automatic':
        dennisov_type = 'nau_automatic'
    elif dennisov_type_selection == 'Lady Flower':
        dennisov_type = 'lady_flower'
    elif dennisov_type_selection == 'Enigma':
        dennisov_type = 'enigma'
    elif dennisov_type_selection == 'Number One':
        dennisov_type = 'number_one'

    dennisov_url = 'https://denissov.ru/en/'+ dennisov_type + '/'
    dennisov_url_subpage = dennisov_url[19:]

    ## Make the html document just created equal to an opened and read URL????
    ##???????????????????????????????????????????????????????????????????
    dennisov_html = urlopen(dennisov_url).read()
    ## Replace instances of double quotation marks in the text with singles 
    ## so that the findall regex code does not get confused 
    dennisov_html = dennisov_html.replace('"', "'")

    ## Find all of the images of the watches. Each watch image starts with the text
    ## "img src=". Do not match those with any " symbols in the URL
    watch_image_urls = findall("<img src='(/files/collections/o[^']*)'", dennisov_html)
    ## Add the URL domain to each watch image subpage to create full addresses
    watch_image_urls = ['https://denissov.ru' + remainder for remainder in watch_image_urls]

    ##    dennisov_file.write('        <img src="' + image + '">\n')

    ## Return the watch type. The watch type is in a title tag called "titlusref"
    ## and can be any combination of letters and spaces, followed by a space and
    ## "<" symbol. 
    watch_type = findall("titlusref'\>([a-zA-Z]+ *[a-zA-Z]*) *\<", dennisov_html)[0]

    ## Find all of the links when each watch is clicked. Each watch link starts
    ## with the text "a href=" followed by the subpage, followed by any
    ## letter, number and "_" symbol combination, followed by a backslash
    watch_link_urls = findall("a href='" + (dennisov_url_subpage) + "([A-Za-z0-9_]+/)", dennisov_html)  
    ## Add the main URL to each watch subpage
    watch_link_urls = [str(dennisov_url) + remainder for remainder in watch_link_urls]

    ## Find all of the model numbers of each watch. Each model starts with the text
    ## "covername" then any combination of letters, dots and spaces. 
    watch_models = findall("covername'>([A-Z a-z0-9\.]+)", dennisov_html)
    ## Add the watch type to each watch model, seperated by a space, to create the
    ## full watch names
    watch_names = [str(watch_type) + " " + remainder for remainder in watch_models]

    ## Get current USD to AUD exchange rate using a known currency website
    currency_converter_url = 'http://www.xe.com/currencyconverter/convert/?From=USD&To=AUD'
    currency_html = urlopen(currency_converter_url).read()
    ## Replace instances of double quotation marks in the text with singles 
    ## so that the findall regex code does not get confused 
    currency_html = currency_html.replace('"', "'")
    ## Find the exchange rate. The exchange rate starts with "uccResultAmount'>"
    ## and is then followed by any combination of numbers with a decimal place
    exchange_rate = float(findall("uccResultAmount'\>([0-9]+\.[0-9]*)", currency_html)[0])

    ## Find the price of the models and make into floats. Each model price contains
    ## numbers followed by the text "USD"
    USD_watch_price = [float(price) for price in (findall("([0-9]*) usd", dennisov_html))]
    ## Convert the USD watch prices to current AUD prices and round to 2 decimals
    watch_price = [round(exchange_rate*price, 2) for price in USD_watch_price]
    ## Add the currency to the prices
    watch_price = ["AU $" + str(price) for price in watch_price]

    ## Match each watch name to its image and URL inside a tuple and place each
    ## tuple inside a list
    watch_list = zip(watch_names, watch_image_urls, watch_price, watch_link_urls)
    ## For each watch tuple (matching image, name and URL), assign a watch number
    watch_option = {'watch_{}'.format(i): e for i, e in enumerate(watch_list)}

## Create a spinbox 
    spinbox_list = []
    spinbox_grid_list = []
    for watch, number in enumerate(watch_names):
        spinbox = 'spinbox_' + str(watch) + ' = Spinbox(window, width = 5, from_=0, to=10)'
        spinbox_grid = 'spinbox_' + str(watch) + '.grid(padx = 2, pady = 2, row = ' + str(1+ 4*watch) +', column = 0)'
        spinbox_list.append(spinbox)
        spinbox_grid_list.append(spinbox_grid)
        spinbox_option = {'spinbox_{}'.format(i):e for i, e in enumerate(spinbox_list)}
    spinbox_option_string = '\n'.join(spinbox_option.values())
    spinbox_grid_option = {'spinbox_grid_{}'.format(i):e for i, e in enumerate(spinbox_grid_list)}
    spinbox_grid_option_string = '\n'.join(spinbox_grid_option.values())
    exec(spinbox_option_string)
    exec(spinbox_grid_option_string)

## Create the dropdown box for the Dennisov watch types
dennisov_type_dropdown = Combobox(window, width = 25,
                                  values = dennisov_type_list)

## Create the Dennisov model selection canvas
dennisov_model_selection = Canvas(window, width = 400, height = 500,
                                  bg = 'white')

## Create Dennisov type selection button
dennisov_select_button = Button(window, text = 'Select',
                                command = display_choice)

## Locate elements on grid
dennisov_type_dropdown.grid(pady = 2, padx = 2, row = 0, column = 1)
dennisov_model_selection.grid(padx = 2, pady = 2, row = 1,
                              column = 0, rowspan = 15, columnspan = 3)
dennisov_select_button.grid(pady = 2, padx = 2, row = 0, column = 2)

dennisov_file.close()
window.mainloop()

None of your dennisov_type_selection == ... conditions will evaluate to True . 您所有的dennisov_type_selection == ...条件都不会评估为True dennisov_type_selection always ends with a newline, and none of the strings you're comparing it against end with a newline. dennisov_type_selection始终以换行符结尾,您要与之进行比较的所有字符串都不以换行符结尾。

Since no conditional executes, nothing is assigned to dennisov_type , so dennisov_url = 'https://denissov.ru/en/'+ dennisov_type + '/' fails with "local variable 'dennisov_type' referenced before assignment 由于没有条件执行,因此没有为dennisov_type分配任何dennisov_type ,因此dennisov_url = 'https://denissov.ru/en/'+ dennisov_type + '/'失败,并"local variable 'dennisov_type' referenced before assignment"local variable 'dennisov_type' referenced before assignment

Try changing the assignment to 尝试将分配更改为

dennisov_type_selection = dennisov_type_dropdown.get()

You should also add an else clause to your if-elif block to handle what happens when the user enters something that you didn't expect. 您还应该在if-elif块中添加else子句,以处理当用户输入您不期望的内容时发生的情况。 Either crash with a useful message, or return from the function early, or assign some kind of fallback value. 崩溃或显示有用的消息,或者提早从函数返回,或者分配某种后备值。

elif dennisov_type_selection == 'Number One':
    dennisov_type = 'number_one'
else:
    raise Exception("Did not recognize selection: {}".format(repr(dennisov_type_selection)))

Or 要么

elif dennisov_type_selection == 'Number One':
    dennisov_type = 'number_one'
else:
    return

Or 要么

elif dennisov_type_selection == 'Number One':
    dennisov_type = 'number_one'
else:
    dennisov_type = 'unknown'

暂无
暂无

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

相关问题 为什么我在赋值之前会引用局部变量? - why am I getting local variable referenced before assignment? 我收到赋值之前局部变量Cruty引用的错误 - I am getting the error that the local variable cruty referenced before assignment 为什么我收到错误:UnboundLocalError: local variable 'lcm' referenced before assignment - Why am I getting the error: UnboundLocalError: local variable 'lcm' referenced before assignment 为什么我会收到此错误“在赋值之前引用了局部变量‘文本’” - Why am I getting this error "local variable 'text' referenced before assignment" 如何使 dataframe 全球化? 出现错误 - UnboundLocalError:分配前引用的局部变量 - How can I make a dataframe global? Getting error - UnboundLocalError: local variable referenced before assignment 为什么在Python中赋值之前会收到UnboundLocalError消息,该消息指出局部变量“参与者”被引用? - Why am I getting the UnboundLocalError that says local variable 'participants' referenced before assignment in Python? 运行以下代码时,我收到此错误“分配前引用的局部变量 'col'”: - I am getting this error “local variable 'col' referenced before assignment” when running the following code: 我收到此错误:“UnboundLocalError:分配前引用的局部变量‘Requesting_books’” - i am getting this error : " UnboundLocalError: local variable 'Requesting_books' referenced before assignment " 我收到一个错误:“UnboundLocalError:分配前引用了局部变量'text_to_print'” - I am getting an error: “UnboundLocalError: local variable 'text_to_print' referenced before assignment” 我一直在分配之前引用此错误局部变量&#39;url&#39; - I keep getting this error local variable 'url' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM