# Codes and their values code1 = ['B', 'F', 'P', 'V'] code2 = ['C', 'G', 'J', 'K', 'Q', 'S', 'X', 'Z'] code3 = ['D', 'T'] code4 = ['L'] code5 = ['M', 'N'] code6 = ['R'] vowels = ['A', 'E', 'I', 'O', 'U'] surname = '' inApp = True while(inApp): while(len(surname) < 1): surname = raw_input("Please input a surname>") surname = surname.upper() soundex = surname[0] prevd = '' # Go through each letter for i in range(0,len(surname)): l = surname[i]; if l in code1: d = '1' elif l in code2: d = '2' elif l in code3: d = '3' elif l in code4: d = '4' elif l in code5: d = '5' elif l in code6: d = '6' elif l in vowels: d = 'v' elif l in ['H', 'W']: d = 'h' else: d = '' # Rule #1 and #2 if (soundex[-1] != d and i > 0): if(prevd != d): soundex += d prevd = d # Rule 3 temp ='' skip = False for i in range(0, len(soundex)): if not skip: # v ########### if soundex[i] == 'v': if( (i - 1) >= 0 and (i + 1) < len(soundex) ): if(soundex[i-1] == soundex[i+1]): temp += soundex[i+1] i+=1 # h ################### elif soundex[i] == 'h': if( (i - 1) >= 0 and (i + 1) < len(soundex) ): if(soundex[i-1] not in ['v','h'] ) and soundex[i+1] not in ['v','h']: if(soundex[i-1] == soundex[i+1]): skip = True #else: # temp += soundex[i+1] #i+=1 else: temp += soundex[i] else: skip = False; soundex = temp; # Make sure it isthe right size if len(soundex) < 4: for i in range (0, 4-len(soundex)): soundex += '0' else: soundex = soundex[:4] print soundex #Exit? t = raw_input("Enter another name? (Y or N)") if(t.upper() != 'Y'): inApp = False else: soundex = '' surname = '' temp = ''