Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环,验证中的错误[Python]_Python_Validation_Loops - Fatal编程技术网

循环,验证中的错误[Python]

循环,验证中的错误[Python],python,validation,loops,Python,Validation,Loops,我正在尝试验证我的python脚本: def DOBSearch(): loop = True while loop == True: try: DOBsrch = int(input("Please enter the birth month in a two digit format e.g. 02: ")) for row in BkRdr: DOB = row[6] day,month,year =

我正在尝试验证我的python脚本:

def DOBSearch():
loop = True
while loop == True:
    try:
        DOBsrch = int(input("Please enter the birth month in a two digit format e.g. 02: "))
        for row in BkRdr:
            DOB = row[6]
            day,month,year = DOB.split("/")
            if DOBsrch == int(month): 
                print("W")
                surname = row[0]
                firstname = row[1]
                print(firstname, " ",surname)
                addrsBk.close
                loop = False
            else:
                print("1 That was an invalid choice please try again.")
    except:
        print("That was an invalid choice please try again.")
但是,当我尝试测试脚本时,我发现有一个bug/错误,因为我得到了以下输出:

     Please enter the birth month in a two digit format e.g. 02: ergase
That was an invalid choice please try again.
Please enter the birth month in a two digit format e.g. 02: 34
1 That was an invalid choice please try again.
1 That was an invalid choice please try again.
1 That was an invalid choice please try again.
1 That was an invalid choice please try again.
1 That was an invalid choice please try again.
1 That was an invalid choice please try again.
Please enter the birth month in a two digit format e.g. 02: 234567
Please enter the birth month in a two digit format e.g. 02: 02
Please enter the birth month in a two digit format e.g. 02: 02
Please enter the birth month in a two digit format e.g. 02: 02
Please enter the birth month in a two digit format e.g. 02:  
以下是CSV文件:

Jackson,Samantha,2 Heather Row,Basingstoke,RG21 3SD,01256 135434,23/04/1973,sam.jackson@hotmail.com
Vickers,Jonathan,18 Saville Gardens,Reading,RG3 5FH,01196 678254,04/02/1965,the_man@btinternet.com
Morris,Sally,The Old Lodge, Hook,RG23 5RD,01256 728443,19/02/1975,smorris@fgh.co.uk
Cobbly,Harry,345 The High Street,Guildford,GU2 4KJ,01458 288763,30/03/1960,harry.cobbly@somewhere.org.uk
Khan,Jasmine,36 Hever Avenue,Edenbridge,TN34 4FG,01569 276524,28/02/1980,jas.khan@hotmail.com
Vickers,Harriet,45 Sage Gardens,Brighton,BN3 2FG,01675 662554,04/04/1968,harriet.vickers@btinternet.com

如果DOBsrch==int(月):中的
else
似乎放错了位置。您建议在此处使用try-except检查值的类型,例如int、string等。。。当else确保它是一个有效的月份选择时,请尝试检查引发的异常。通常,您希望避免使用普通的
except
块,而是指定可以从代码中引发的异常类型。如果你捕获了所有内容,你可以隐藏你没有预料到的错误。有人可以发布答案或替代解决方案吗?你知道实际打印的是哪个
print
语句吗?
else
下的那一个,还是
下除
以外的那一个?