Reputation: 367
I am trying to validate my python script:
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 ValueError:
print("That was an invalid choice please try again.")
However when I try to test the script I find there is a bug/error as I get the following outputs:
>>> DOBSearch()
Please enter the birth month in a two digit format e.g. 02: 2345
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: 23456
Please enter the birth month in a two digit format e.g. 02: 4321
Please enter the birth month in a two digit format e.g. 02: 2345
Please enter the birth month in a two digit format e.g. 02: yrsdhctg
That was an invalid choice please try again.
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:
here is the CSV file:
Jackson,Samantha,2 Heather Row,Basingstoke,RG21 3SD,01256 135434,23/04/1973,[email protected]
Vickers,Jonathan,18 Saville Gardens,Reading,RG3 5FH,01196 678254,04/02/1965,[email protected]
Morris,Sally,The Old Lodge, Hook,RG23 5RD,01256 728443,19/02/1975,[email protected]
Cobbly,Harry,345 The High Street,Guildford,GU2 4KJ,01458 288763,30/03/1960,[email protected]
Khan,Jasmine,36 Hever Avenue,Edenbridge,TN34 4FG,01569 276524,28/02/1980,[email protected]
Vickers,Harriet,45 Sage Gardens,Brighton,BN3 2FG,01675 662554,04/04/1968,[email protected]
Upvotes: 0
Views: 66
Reputation: 122061
You aren't distinguishing between what is a valid birth month (i.e. one that makes sense, an integer between 1 and 12) and what is a birth month in BkRdr
(presumably, some subset of that). You have too much in the try
block, and will tell the user for every row
in BkRdr
that their input was invalid, when a later row
might actually match.
I would split this into at least two functions, with explicit arguments rather than relying on scope:
def input_month():
"""Get a valid birth month from the user."""
while True:
try:
month = int(input("Please enter the birth month in a two digit format e.g. 02: "))
except ValueError:
print("Must be a number")
else:
if month in range(1, 13):
return month
print("Must be in range 1-12")
def DOB_search(BkRdr):
"""Find a valid birth month in BkRdr."""
while True:
search_month = input_month()
for row in BkRdr:
...
if search_month == int(month):
...
return " ".join((firstname, surname))
print("Not found in BkRdr.")
This separates the two concerns of your code:
BkRdr
.I would also rethink the variable names - what is a BkRdr
anyway?
Upvotes: 1
Reputation: 104722
I think there are a two errors in your code.
The first is that you're printing your first error message repeatedly. This is because the print
line is inside of the for
loop that checks against each row from your CSV file. Each row that doesn't match produces the error message.
To fix that, you should probably move the print
statement up a level, to be called after the for
loop ends.
The second issue is that BkRdr
, which you loop on, appears to be an iterator, not a sequence that you can repeatedly iterate. This is why every numerical entry after the first fails silently. The for
loop doesn't do anything at all in those cases!
To fix this second issue, you probably need to change other code you haven't shown, so that your CSV file gets read into a list, rather than only an iterator. (Or, alternatively, you could rewind the file, or close and reopen it.)
Upvotes: 0