I want to search for specific value and return the row information. If values cannot be found, I want the program to return "Not found".
The problem is if the value is not found, program will print "Not found" the same number of times as the number of rows. How can i make the program return only one time if values is not in CSV file?
with open(filePath) as csvfile: reader = csv.reader(csvfile)
avatarIdInput = input("Enter Avatar ID: ")
for row in reader:
if(avatarIdInput.upper() == row[2]):
print("===============================================")
print("{0:<10}|{1:>12} |{2:>13}".format("Avatar ID", "Avatar Name", "Avatar Tribe"))
print("{0:<10}|{1:>12} | {2}".format(row[2], row[0], row[1]))
print("===============================================")
print("Four Elements Power")
print("===============================================")
print("{0:<4}|{1:>6} |{2:>6} |{3:>5} |{4:>14}".format("Air", "Water", "Earth", "Fire", "Average Power"))
averagePower = (int(row[3]) + int(row[4]) + int(row[5]) + int(row[6]))/4
print(" {0:<3}|{1:>6} |{2:>6} |{3:>5} |{4:^14}".format(row[3], row[4], row[5], row[6], averagePower))
print("===============================================")
elif (avatarIdInput != row[2]):
print("Not found")
Aucun commentaire:
Enregistrer un commentaire