我希望你能再一次帮助我,我仍然是编程的老手,所以请耐心等待。我收到了这个错误:
Traceback (most recent call last):
File "C:\Python27\cx_Freeze exe Creator\Postcodezoekernl.py", line 136, in <module>
postcodeinvoer = int(postcodeinvoer)
TypeError: int() argument must be a string or a number, not 'NoneType'
该程序有效,但是当我编译它时,它会在你关闭程序时发出错误,说代码有问题。我已经尝试删除该行但该程序在没有它的情况下不会工作,因为它将直接进入ELSE语句。 Postcode1是某些数字之间的范围。用户必须填写一个号码。如果数字在该范围内,它将显示msgbox。
elif keuze in week2:
postcodeinvoer = easygui.enterbox(msg="Voer een postcode in:", title="Postcodezoeker")
postcodeinvoer = int(postcodeinvoer)
if postcodeinvoer in postcode1:
easygui.msgbox(msg="[Woensdag 3 mei, 10 mei, 17 mei, 24 mei] [Donderdag 4 mei, 11 mei, 18 mei]", title=postcodeinvoer)
elif postcodeinvoer in postcode2:
easygui.msgbox(msg="[Dinsdag 2 mei, 9 mei, 16 mei, 23 mei] [Donderdag 4 mei, 11 mei, 18 mei]", title=postcodeinvoer)
elif postcodeinvoer in postcode3:
easygui.msgbox(msg="[Dinsdag 2 mei, 9 mei, 16 mei, 23 mei] [Donderdag 4 mei, 11 mei, 18 mei]", title=postcodeinvoer)
顺便说一句,是否可以将python文件编译为单个.exe文件? 希望你们能帮助我。提前谢谢!
问题解决了!
答案 0 :(得分:0)
int(something)
如果可以从它的参数中设置int,则会引发TypeError
或ValueError
,因此最简单的解决方案是捕获异常并重试。但由于我们希望允许用户取消操作,我们仍然会再次测试None
并退出
cancel = False
basemsg = msg = "Voer een postcode in:"
while True:
postcodeinvoer = easygui.enterbox(msg=msg, title="Postcodezoeker")
if postcodeinvoer is None:
# user canceled
cancel = True
break
try:
postcodeinvoer = int(postcodeinvoer)
except (TypeError, ValueError) as e:
msg = "invalid value ! " + basemsg
else:
# ok, let's get out
break
if cancel:
# exit the program or whatever...
raise SysExit()
# ok, proceed with the user's value
现在你似乎犯了一个常见的新手错误,就是认为如果某些事情由数字组成,那么它就是一个数字,而事实并非如此。邮政编码或电话号码通常是字符串,而不是整数 - 问问自己将电话号码乘以2并将结果除以邮政编码会有什么意义;)
我不应该将postcodeinvoer
转换为int
。如果您所在的国家/地区的邮政编码仅由数字组成(在所有国家/地区都不是这种情况),那么您可以测试用户输入是由N
位组成的字符串( N
的值取决于您所在的国家/地区:
postcodeinvoer = easygui.enterbox(msg=msg, title="Postcodezoeker")
if postcodeinvoer is None:
# user canceled
cancel = True
break
if (postcodeinvoer.isdigit() and len(postcodeinvoer) == N):
# valid code
break
else:
# invalid code, ask again
msg = "invalid value ! " + basemsg
但即便如此,它可能还不够好......
答案 1 :(得分:-3)
检查postcodeinvoer
不是无。
if postcodeinvoer:
postcodeinvoer = int(postcodeinvoer)