I have a problem in which I have a function which takes in an ID, and then checks for any invalid form of format within the ID.
The rules are:
1) ID must have a length of 5
2) The ID must start with a letter depending on the tribe name
3) The ID must end with a valid letter depending on average power
4) Contains three integers in between the two letters
ID Format: TNNNL In which: T - tribe name N - A digit L - A letter corresponding to the average power of an avatar
csv file:
name,tribe,id,Air,Water,Earth,Fire,Rule to test
Pema,Xero,X14C,24,54,34,43,Length of Avatar ID is not 5
Otaku,Taru,T111F,54,78,65,78,Invalid last letter
Aang,Nomad,NA21B,89,67,54,78,Invalid ID format
Zuko,Giant,A111C,48,54,98,75,Invalid first letter
My code:
import csv
def isValidAvatarIDFormat(ava_id):
filePath = "data1.csv"
with open(filePath) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
tribe = row['tribe']
air_power = row['Air']
water_power = row['Water']
earth_power = row['Earth']
fire_power = row ['Fire']
#check id length
if(len(ava_id) != 5):
return("Length of ID is not 5")
#check first letter
elif(ava_id[0] != tribe[0]):
return("Invalid first letter")
#check last letter
elif(ava_id[4] != findAveragePower(air_power, water_power, earth_power, fire_power)):
return("Invalid last letter")
#check 3 digits in the middle
elif bool(ava_id[1].isdigit() and ava_id[2].isdigit() and ava_id[3].isdigit()) == False:
return("Invalid ID format")
def findAveragePower(air_power, water_power, earth_power, fire_power):
air_power = row['Air']
water_power = row['Water']
earth_power = row['Earth']
fire_power = row ['Fire']
average = int(int(air_power) + int(water_power) + int(earth_power) + int(fire_power)) / 4
if(average >= 80):
return "A"
if(average >= 70 and average < 80):
return "B"
if(average >= 60 and average < 70):
return "C"
if(average >= 50 and average < 60):
return "D"
if(average >= 40 and average < 50):
return "E"
if(average <40):
return "F"
#Main Program:
filePath = "data1.csv"
with open(filePath) as csvfile:
reader = csv.DictReader(csvfile)
print("{0:<5} | {1:^5} | {2:^5}".format("Avatar Name", "Avatar ID", "Comments"))
for row in reader:
string = isValidAvatarIDFormat(row['id'])
print("{0:<11} | {1:<9} | {2:<15}".format(row['name'],row['id'], string))
Expected Output: I had expected the output to be the same as what was under the 'Rule to test' column in the CSV file. However the output was much more different than i expected.
Actual Output:

