关闭时出现Python程序错误

2024-09-22 20:39:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望你能再次帮助我,我还不懂编程,所以请你忍受我。我有个错误:

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语句。邮政编码1是特定数字之间的范围。用户必须填写一个数字。如果数字在该范围内,它将显示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文件? 希望你们能帮我。提前谢谢!你知道吗

问题解决了!你知道吗


Tags: in程序title错误msg数字exeint
2条回答

int(something)将引发TypeErrorValueError,如果它可以从其参数实例化int,那么最简单的解决方案是捕获异常并重试。但是,由于我们希望允许用户取消操作,因此我们仍将测试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

但即便如此,这也可能不是一个很好的验证。。。你知道吗

Checkpostcodeinvoer不是无。你知道吗

if postcodeinvoer:
    postcodeinvoer = int(postcodeinvoer)

相关问题 更多 >