python的try和except用法_python – 使用try和except语句赋值之前引用的局部变量

参见英文答案 >

local variable ‘servers’ referenced before assignment 2个

我在try和except语句中遇到一些问题,我有一个条目小部件,它接受字符串输入,但我有代码,后来将它转换为整数,问题是如果用户输入类似文本的东西,它会抛出这样的错误:

Exception in Tkinter callback

Traceback (most recent call last):

File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__

return self.func(*args)

File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 157, in buttonclick_gamescreen

entryx = int(e1.get())

ValueError: invalid literal for int() with base 10: 'abc'

所以我想用try和except语句隐藏错误但我现在得到另一条错误消息.

这就是它在代码中的样子.

while pressed == 8 :

try:

entryx = int(e1.get())

entryy = int(e2.get())

except ValueError:

print("text")

answerx = answerlistx[randomimage]

answery = answerlisty[randomimage]

if entryx == answerx and entryy == answery

canvas.delete(images)

randomimage = random.randrange(0,49+1)

scorecounter = scorecounter + 1

game = PhotoImage(file=imagelist[randomimage])

images = canvas.create_image(30, 65, image = game, anchor = NW)

e1.delete(0, END)

e2.delete(0, END)

pressed = ''

if entryx > 10 or entryx < -10 or entryy > 10 or entryy < -10 :

wrong = canvas.create_image(30, 65, image = outside, anchor = NW)

e1.delete(0, END)

e2.delete(0, END)

pressed = ''

else:

wrong = canvas.create_image(30, 65, image = incorrect, anchor = NW)

e1.delete(0, END)

e2.delete(0, END)

pressed = ''

新的错误消息:

text

Exception in Tkinter callback

Traceback (most recent call last):

File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__

return self.func(*args)

File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 165, in buttonclick_gamescreen

if entryx == answerx and entryy == answery:

UnboundLocalError: local variable 'entryx' referenced before assignment

我无法弄清楚为什么会发生这种情况以及如何解决它所以任何帮助将不胜感激.

提前致谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值