【问题标题】:Python trouble with returning value of a variablePython返回变量值的问题
【发布时间】:2017-02-15 10:23:14
【问题描述】:

当我运行第一个函数时,它会打印正确的 URL。但是,当我运行第二部分时,它显示一个错误,指出未定义 fullurl。

谁能帮我解决这个问题?

这是我的代码:

def urlmaker(format_mtg):
    fullurl = url + format_mtg.get() + "-constructed-league-" + date.get() #adds the users options to the url
    print(fullurl)
    return fullurl

def htmltotxt(fullurl):
    print(fullurl)
    response = urllib.request.urlopen(fullurl) #requests the ability to open the website, which magic.wizards.com allows
    html = response.read() #reads the html data from the open website
    html = str(html) #saves the data as a string
    make_lists(card_name_regex, card_number_regex, card_number_list, html)

【问题讨论】:

  • 请使用空格。 Python 不按字符收费。
  • 另外,显示实际的完整错误和回溯。您发布的代码中没有任何内容会给出该错误。
  • 您还没有展示如何调用这些函数。请也包括在内。另外,请检查您的缩进。

标签: python function python-3.x return


【解决方案1】:

您的代码没有 cmets 并且具有适当的缩进和间距:

def urlmaker(format_mtg):
    fullurl = url + format_mtg.get() + "-constructed-league-" + date.get()
    print(fullurl)
    return fullurl

def htmltotxt(fullurl):
    print(fullurl)
    response = urllib.request.urlopen(fullurl)
    html = response.read()
    html = str(html)
    make_lists(card_name_regex, card_number_regex, card_number_list, html)
  1. 粘贴必要的导入(在所示代码中使用的那些),至少应该导入urllib
  2. 您正在使用 5 个我们不知道的变量:urldatecard_name_regexcard_number_regexcard_number_listdate 甚至可能不是变量,而是导入的东西。定义它们的值或给出一个示例值,以便我们重现您的错误。
  3. 您没有展示如何调用函数,因此我们不知道format_mtgfullurl 参数的值。我可以推断您正在使用第一个函数的结果作为第二个函数的参数,但仍然在测量 format_mtg
  4. 粘贴您遇到的异常,以便我们为您提供帮助。

没有这 4 件事,我们无法找到您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多