鱼C论坛

 找回密码
 立即注册
查看: 1130|回复: 2

[已解决]爬一只喵出问题了...

[复制链接]
发表于 2018-3-4 12:02:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
054讲  课后题动动手0

  1. import easygui as g
  2. import urllib.request as u

  3. def b():
  4.     width,high = g.multenterbox('请填写喵的尺寸','下载一只喵',['宽:','高:'],[400,600])
  5.     path = g.diropenbox('请选择存放喵的文件夹')

  6. def c():
  7.     response = u.urlopen('http://www.placekitten.com/g/' + width + '/' + high)
  8.     cat_img = response.read()

  9.     with open(path + '\\cat_' + width + '_' + high + '.jpg','wb') as j:
  10.         j.write(cat_img)

  11. def a():
  12.     try:
  13.         b()
  14.     except TypeError:
  15.         g.msgbox('未选择存放文件夹,请重新选择!')
  16.         a()
  17.     else:
  18.         c()

  19. a()
复制代码


NameError: name 'width' is not defined
为什么明明运行了b(),width、high、path的依然没有被赋值呢?
最佳答案
2018-3-5 16:01:40
两个方法
1、声明全局变量,即在def方法体外面什么width 和high
  1. import easygui as g
  2. import urllib.request as u

  3. width = 0
  4. high = 0

  5. def b():
  6.     width,high = g.multenterbox('请填写喵的尺寸','下载一只喵',['宽:','高:'],[400,600])
  7.     path = g.diropenbox('请选择存放喵的文件夹')
复制代码

2、运行b()是返回width和high,调用c()的时候传进去
  1. import easygui as g
  2. import urllib.request as u

  3. def b():
  4.     width,high = g.multenterbox('请填写喵的尺寸','下载一只喵',['宽:','高:'],[400,600])
  5.     path = g.diropenbox('请选择存放喵的文件夹')
  6.     return width,high

  7. def c(witdh, high):
  8.     response = u.urlopen('http://www.placekitten.com/g/' + width + '/' + high)
  9.     cat_img = response.read()

  10.     with open(path + '\\cat_' + width + '_' + high + '.jpg','wb') as j:
  11.         j.write(cat_img)

  12. def a():
  13.     try:
  14.         witdh,high = b()
  15.         c(witdh, high)
  16.     except TypeError:
  17.         g.msgbox('未选择存放文件夹,请重新选择!')
  18.         a()
  19. a()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-3-4 12:14:24 | 显示全部楼层
本帖最后由 snail:) 于 2018-3-4 12:23 编辑

我也是新手,我对easygui模块不是很清楚,我只能帮你改下语法,希望可帮到你
  1. import easygui as g
  2. import urllib.request as u


  3.    

  4. def c():
  5.     response = u.urlopen('http://www.placekitten.com/g/' + width + '/' + high)
  6.     cat_img = response.read()

  7.     with open(path + '\\cat_' + width + '_' + high + '.jpg','wb') as j:
  8.         j.write(cat_img)

  9. def a(width,high,path):
  10.     c()
  11. width,high = g.multenterbox('请填写喵的尺寸','下载一只喵',['宽:','高:'],[400,600])
  12. path = g.diropenbox('请选择存放喵的文件夹')
  13. a(width,high,path)
复制代码
在函数里面没有申明是全局变量的,在另一个函数都是不能被直接调用的,你需要把全局变量放到函数外面,或者在函数中申明下,或者用return进行返回,在用变量接收,稍等下,我对你的代码进行修改
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-5 16:01:40 | 显示全部楼层    本楼为最佳答案   
两个方法
1、声明全局变量,即在def方法体外面什么width 和high
  1. import easygui as g
  2. import urllib.request as u

  3. width = 0
  4. high = 0

  5. def b():
  6.     width,high = g.multenterbox('请填写喵的尺寸','下载一只喵',['宽:','高:'],[400,600])
  7.     path = g.diropenbox('请选择存放喵的文件夹')
复制代码

2、运行b()是返回width和high,调用c()的时候传进去
  1. import easygui as g
  2. import urllib.request as u

  3. def b():
  4.     width,high = g.multenterbox('请填写喵的尺寸','下载一只喵',['宽:','高:'],[400,600])
  5.     path = g.diropenbox('请选择存放喵的文件夹')
  6.     return width,high

  7. def c(witdh, high):
  8.     response = u.urlopen('http://www.placekitten.com/g/' + width + '/' + high)
  9.     cat_img = response.read()

  10.     with open(path + '\\cat_' + width + '_' + high + '.jpg','wb') as j:
  11.         j.write(cat_img)

  12. def a():
  13.     try:
  14.         witdh,high = b()
  15.         c(witdh, high)
  16.     except TypeError:
  17.         g.msgbox('未选择存放文件夹,请重新选择!')
  18.         a()
  19. a()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 01:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表