python 递归返回None?

roffer 2015-02-05 10:25:31

city = ['四川','成都','温江']

def getWeatherForCity(city,city_code=''):
if city_code == '':
content = getContent('http://m.weather.com.cn/data5/city.xml')
arr = content.split(',')
for i in arr:
if city[0] == i.split('|')[1]:
city_code = i.split('|')[0]
print city[0] + '的编码是:' + city_code
del city[0]
break
getWeatherForCity(city,city_code)
else:
content = getContent('http://m.weather.com.cn/data5/city%s.xml' % city_code)
arr = content.split(',')
for i in arr:
if city[0] == i.split('|')[1]:
city_code = i.split('|')[0]
print city[0] + '的编码是:' + city_code
del city[0]
break
if city.__len__() > 0:
getWeatherForCity(city,city_code)
else:
result = getContent('http://www.weather.com.cn/data/cityinfo/%s.html' % ('101' + city_code))
print result
return result

def getContent(url):
return urllib2.urlopen(url).read()

print getWeatherForCity(city)


本人昨天刚学python,不知道哪里错了,求指教!
...全文
153 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
DarkChampion 2015-02-06
  • 打赏
  • 举报
回复
每个调用getWeatherForCity(city,city_code)的地方都改成
return getWeatherForCity(city,city_code)
递归返回上一层时结果没继续往上返回
panghuhu250 2015-02-06
  • 打赏
  • 举报
回复
函数体里的(有几个)

        getWeatherForCity(city,city_code)
改成:

        return getWeatherForCity(city,city_code)
roffer 2015-02-05
  • 打赏
  • 举报
回复
引用 3 楼 functionsub 的回复:
import urllib.request as urllib2     
        
city = ['四川','成都','温江']
 
def getWeatherForCity(city,city_code=''):
    if city_code == '':
        content = getContent('http://m.weather.com.cn/data5/city.xml')
        print(content)
        arr = content.split(',')
        for i in arr:
            if city[0] == i.split('|')[1]:
                city_code = i.split('|')[0]
                print(city[0] + '的编码是:' + city_code)
                del city[0]
                break
        getWeatherForCity(city,city_code)
    else:
        content = getContent('http://m.weather.com.cn/data5/city%s.xml' % city_code)
        arr = content.split(',')
        for i in arr:
            if city[0] == i.split('|')[1]:
                city_code = i.split('|')[0]
                print(city[0] + '的编码是:' + city_code)
                del city[0]
                break
        if city.__len__() > 0:
            getWeatherForCity(city,city_code)
        else:
            result = getContent('http://www.weather.com.cn/data/cityinfo/%s.html' % ('101' + city_code))
            print(result)
            return result
     
def getContent(url):
    return urllib2.urlopen(url).readall().decode('utf-8')
 
print(getWeatherForCity(city))
我的python是3.3,把read改成了readall,完了再decode一下,程序返回正常。。 分别是 27,2701,270104
噢噢,不对,看错了,还是不德行。
roffer 2015-02-05
  • 打赏
  • 举报
回复
引用 3 楼 functionsub 的回复:
import urllib.request as urllib2     
        
city = ['四川','成都','温江']
 
def getWeatherForCity(city,city_code=''):
    if city_code == '':
        content = getContent('http://m.weather.com.cn/data5/city.xml')
        print(content)
        arr = content.split(',')
        for i in arr:
            if city[0] == i.split('|')[1]:
                city_code = i.split('|')[0]
                print(city[0] + '的编码是:' + city_code)
                del city[0]
                break
        getWeatherForCity(city,city_code)
    else:
        content = getContent('http://m.weather.com.cn/data5/city%s.xml' % city_code)
        arr = content.split(',')
        for i in arr:
            if city[0] == i.split('|')[1]:
                city_code = i.split('|')[0]
                print(city[0] + '的编码是:' + city_code)
                del city[0]
                break
        if city.__len__() > 0:
            getWeatherForCity(city,city_code)
        else:
            result = getContent('http://www.weather.com.cn/data/cityinfo/%s.html' % ('101' + city_code))
            print(result)
            return result
     
def getContent(url):
    return urllib2.urlopen(url).readall().decode('utf-8')
 
print(getWeatherForCity(city))
我的python是3.3,把read改成了readall,完了再decode一下,程序返回正常。。 分别是 27,2701,270104
感谢楼上,果然是decoude的原因(return result.decode('utf8')),但是为什么呢???不解!
functionsub 2015-02-05
  • 打赏
  • 举报
回复
import urllib.request as urllib2     
        
city = ['四川','成都','温江']
 
def getWeatherForCity(city,city_code=''):
    if city_code == '':
        content = getContent('http://m.weather.com.cn/data5/city.xml')
        print(content)
        arr = content.split(',')
        for i in arr:
            if city[0] == i.split('|')[1]:
                city_code = i.split('|')[0]
                print(city[0] + '的编码是:' + city_code)
                del city[0]
                break
        getWeatherForCity(city,city_code)
    else:
        content = getContent('http://m.weather.com.cn/data5/city%s.xml' % city_code)
        arr = content.split(',')
        for i in arr:
            if city[0] == i.split('|')[1]:
                city_code = i.split('|')[0]
                print(city[0] + '的编码是:' + city_code)
                del city[0]
                break
        if city.__len__() > 0:
            getWeatherForCity(city,city_code)
        else:
            result = getContent('http://www.weather.com.cn/data/cityinfo/%s.html' % ('101' + city_code))
            print(result)
            return result
     
def getContent(url):
    return urllib2.urlopen(url).readall().decode('utf-8')
 
print(getWeatherForCity(city))
我的python是3.3,把read改成了readall,完了再decode一下,程序返回正常。。 分别是 27,2701,270104
roffer 2015-02-05
  • 打赏
  • 举报
回复
引用 1 楼 l821398100 的回复:
简单看了一下你的代码,你的代码只有一个地方return了 ,查看一下你return的结果是否本来就是空的 ?
print result 是有值的。
小雷同学 2015-02-05
  • 打赏
  • 举报
回复
简单看了一下你的代码,你的代码只有一个地方return了 ,查看一下你return的结果是否本来就是空的 ?

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧