I am doing an assignment and everything is going well except my final if/else statement defaults to the else block unless I enter the first value I am trying to check and I think I understand why but I can't think of a way to prevent it. However, when I run the program without the else block the output is perfectly fine.
Input file:
LE1 Leicester
LE2 Oadby,Knighton,Highfields,Aylestone
LE3 Braunstone,Glenfield,Groby Road
LE4 BeaumontLeys,Belgrave,Birstall,Thurmaston
LE5 Hamilton,ThurnbyLodge,Evington
Code:
def area3(filename):
f = open(filename, "r")
aList = list()
bList = list()
for line in f:
line = line.strip()
f = line.split("\t")
aList.append(f)
for line in aList:
for i in line[1:]:
i = i.split(",")
bList.append(i)
for i in range(0, len(aList)):
del aList[i][1]
aList[i].append(bList[i])
for j in aList:
for x in j[1]:
print(j[0], x)
print("")
x = input("Enter the name of the suburb to get its postcode: ")
x = x.capitalize()
for i in aList:
for j in i[1]:
if x == j:
return "The postcode is: " + i[0]
else:
return "Not Found"
print(area3(input("Input filename: ")))
Output with the else block:
Input filename: postcode.txt
LE1 Leicester
LE2 Oadby
LE2 Knighton
LE2 Highfields
LE2 Aylestone
LE3 Braunstone
LE3 Glenfield
LE3 Groby Road
LE4 BeaumontLeys
LE4 Belgrave
LE4 Birstall
LE4 Thurmaston
LE5 Hamilton
LE5 ThurnbyLodge
LE5 Evington
Enter the name of the suburb to get its postcode: evington
Not Found
Output without the else block:
Input filename: postcode.txt
LE1 Leicester
LE2 Oadby
LE2 Knighton
LE2 Highfields
LE2 Aylestone
LE3 Braunstone
LE3 Glenfield
LE3 Groby Road
LE4 BeaumontLeys
LE4 Belgrave
LE4 Birstall
LE4 Thurmaston
LE5 Hamilton
LE5 ThurnbyLodge
LE5 Evington
Enter the name of the suburb to get its postcode: evington
The postcode is: LE5
Any help to fix this would be much appreciated.
Aucun commentaire:
Enregistrer un commentaire