Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为什么我得到';UnboundLocalError:局部变量';ask2和x27;作业前参考';错误?_Python 3.x - Fatal编程技术网

Python 3.x 为什么我得到';UnboundLocalError:局部变量';ask2和x27;作业前参考';错误?

Python 3.x 为什么我得到';UnboundLocalError:局部变量';ask2和x27;作业前参考';错误?,python-3.x,Python 3.x,这是我的网页抓取代码。我可以毫无问题地运行它,而无需将所有逻辑放入单个def函数中。但是我想把所有的逻辑放到一个def函数中 from urllib.request import urlopen as UReq from bs4 import BeautifulSoup as Bsoup def check(author, aquote): ask3 = str(input("Do you want to save this line in a text?\n- (Yes/

这是我的网页抓取代码。我可以毫无问题地运行它,而无需将所有逻辑放入单个
def
函数中。但是我想把所有的逻辑放到一个
def
函数中

from urllib.request import urlopen as UReq

from bs4 import BeautifulSoup as Bsoup

def check(author, aquote):
    ask3 = str(input("Do you want to save this line in a text?\n- (Yes/y) "))
    if ask3.lower().strip() == "yes" or ask3.upper().strip() == "Y":
        SAVE_AS_TXT(quote=aquote, author=author)


def Printing():
    print()
    Printing.ask1 = str(input("Do you want to save all of these in a text?\n- (Yes/y) "))
    if Printing.ask1.lower().strip() == "yes" or Printing.ask1.upper().strip() == "Y":
        Printing.keyAuthor = []
        Printing.values = []
    else:
        ask2 = str(input("Do you want to save `line by line` in a text?\n- (Yes/y) "))
        if ask2.lower().strip() == "yes" or ask2.upper().strip() == "Y":
            ask2.upper()
    for quote in quotes:
        fav_quote = quote.findAll("p", {"class": "aquote"})
        aquote = f'{fav_quote[0].text.strip().capitalize()}'

        fav_authors = quote.findAll("p", {"class": "author"})
        author = f"- {fav_authors[0].text.strip().upper()}\n"
        print(aquote)
        print(author)

        if ask2 == "YES" or ask2 == "Y":
            check(author=author, aquote=aquote)
        elif Printing.ask1.lower().strip() == "yes" or Printing.ask1.upper().strip() == "Y":
            Printing.keyAuthor.append(author)
            Printing.values.append(aquote)
        else:
            continue


def SAVE_AS_TXT(quote, author):
    with open("Quotes.txt", 'a', encoding="utf-8") as f:
        f.write(f"{quote}\n")
        f.write(f"{author}\n")


quotes_page = 'https://bluelimelearning.github.io/my-fav-quotes/'
UClient = UReq(quotes_page)
page_html = UClient.read()
UClient.close()

page_soup = Bsoup(page_html, "html.parser")
quotes = page_soup.findAll("div", {"class": "quotes"})
print(len(quotes))
# print(quotes[1])
# print(page_soup.h1.text.strip())

Printing()
if Printing.ask1.lower().strip() == "yes" or Printing.ask1.upper().strip() == "Y":
    zipObj = zip(Printing.keyAuthor, Printing.values)
    dictOf = dict(zipObj)

    for key, value in dictOf.items():
        SAVE_AS_TXT(quote=value, author=key)

这是我的代码,我想做一个函数,要求用户将输出保存在文本文件中。它要求
保存所有的
逐行
或只
打印

变量
ask2
的范围是else块的局部范围。然后再次尝试在另一个if块中访问它,但是这里的
ask2
变量范围是该if块的局部范围。该局部变量没有任何赋值,因此错误“赋值前引用”。为了能够从任何一个块访问变量,范围必须是全局的。因此,在第
def Printing()行之后:
add
ask2=''