Python 为什么y没有定义?

Python 为什么y没有定义?,python,python-2.7,urllib2,Python,Python 2.7,Urllib2,在该代码中,由于某种原因,变量y未仅为第一条if语句定义。我需要知道为什么它说y没有定义。如果您需要查找WOEID的代码,也可以在这里找到 import urllib2 def make_word(words): result = "" for i in words: result += i return result continents = ['asia', 'north%20america', 'south%20america', 'europe

在该代码中,由于某种原因,变量
y
未仅为第一条
if
语句定义。我需要知道为什么它说
y
没有定义。如果您需要查找
WOEID
的代码,也可以在这里找到

import urllib2

def make_word(words):
    result = ""
    for i in words:
        result += i
    return result

continents = ['asia', 'north%20america', 'south%20america', 'europe', 'asia', 'australia', 'antarctica']
a = raw_input('Put in a place: ')
a = a.lower()
if ' ' in a:
    for h in a:
        if h == ' ':
            y = a.replace(' ', '%20')

page = urllib2.urlopen('http://woeid.rosselliot.co.nz/lookup/%s' % a).read()
f = page.find('Ohio')
if y not in continents:
    if f != -1:
        start = page.find('woeid="', f) + 7
        end = page.find('"', start)
        print 'The WOEID for %s: %s' % (a, page[start:end])
    if f == -1:
        f = page.find('United States')
        if f != -1:
            start = page.find('woeid="', f) + 7
            end = page.find('"', start)
            print 'The WOEID for %s: %s' % (a, page[start:end])
if y in continents:
    if f == -1:
        f = page.find('')
        start = page.find('woeid="', f) + 7
        print page[start:]
        end = page.find('"', start)
        print 'The WOEID for %s: %s' % (a, page[start:end])

您仅在非常特定的情况下定义
y

if ' ' in a:
    for h in a:
        if h == ' ':
            y = a.replace(' ', '%20')
因此,只有当
a
中有空格时,才能生成
y

不要在这里创建新变量,也不要手动进行URL编码。请使用以下选项:

在整个代码中,您似乎混合了
a
y
。也许您需要在此处使用更有意义的名称:

place = raw_input('Put in a place: ')
place_quoted = quote(place.lower())
page = urllib2.urlopen('http://woeid.rosselliot.co.nz/lookup/%s' % place_quoted).read()
if place_quoted not in continents:

您仅在非常特定的情况下定义
y

if ' ' in a:
    for h in a:
        if h == ' ':
            y = a.replace(' ', '%20')
因此,只有当
a
中有空格时,才能生成
y

不要在这里创建新变量,也不要手动进行URL编码。请使用以下选项:

在整个代码中,您似乎混合了
a
y
。也许您需要在此处使用更有意义的名称:

place = raw_input('Put in a place: ')
place_quoted = quote(place.lower())
page = urllib2.urlopen('http://woeid.rosselliot.co.nz/lookup/%s' % place_quoted).read()
if place_quoted not in continents:

您仅在非常特定的情况下定义
y

if ' ' in a:
    for h in a:
        if h == ' ':
            y = a.replace(' ', '%20')
因此,只有当
a
中有空格时,才能生成
y

不要在这里创建新变量,也不要手动进行URL编码。请使用以下选项:

在整个代码中,您似乎混合了
a
y
。也许您需要在此处使用更有意义的名称:

place = raw_input('Put in a place: ')
place_quoted = quote(place.lower())
page = urllib2.urlopen('http://woeid.rosselliot.co.nz/lookup/%s' % place_quoted).read()
if place_quoted not in continents:

您仅在非常特定的情况下定义
y

if ' ' in a:
    for h in a:
        if h == ' ':
            y = a.replace(' ', '%20')
因此,只有当
a
中有空格时,才能生成
y

不要在这里创建新变量,也不要手动进行URL编码。请使用以下选项:

在整个代码中,您似乎混合了
a
y
。也许您需要在此处使用更有意义的名称:

place = raw_input('Put in a place: ')
place_quoted = quote(place.lower())
page = urllib2.urlopen('http://woeid.rosselliot.co.nz/lookup/%s' % place_quoted).read()
if place_quoted not in continents:
如果a:#中的“”此条件在代码中失败,并且
y
变量未初始化/设置

如果y不在大陆:#当这行执行时,它没有y的引用,抛出错误“undefined”

最好在代码开始时初始化默认值

例如:y=None(或)正确处理变量。

如果a:#中的“”此条件在代码中失败,并且
y
变量未初始化/设置

如果y不在大陆:#当这行执行时,它没有y的引用,抛出错误“undefined”

最好在代码开始时初始化默认值

例如:y=None(或)正确处理变量。

如果a:#中的“”此条件在代码中失败,并且
y
变量未初始化/设置

如果y不在大陆:#当这行执行时,它没有y的引用,抛出错误“undefined”

最好在代码开始时初始化默认值

例如:y=None(或)正确处理变量。

如果a:#中的“”此条件在代码中失败,并且
y
变量未初始化/设置

如果y不在大陆:#当这行执行时,它没有y的引用,抛出错误“undefined”

最好在代码开始时初始化默认值


例如:y=None(或)正确处理变量。

也许他们不知道python变量没有任何默认值,如0或“”。@Paul:即使它有默认值,在发布的代码上下文中也没有意义。也许他们不知道python变量没有任何默认值,例如0或“”。@Paul:即使它有默认值,在发布的代码上下文中也没有意义。也许他们不知道python变量没有任何默认值,例如0或“”。@Paul:即使它有默认值,在发布的代码上下文中没有意义。也许他们不知道python变量没有任何默认值,如0或“”。@Paul:即使它有默认值,在发布的代码上下文中也没有意义。