Building a Name Preference Wizard – Step 4/?
Formerly titled “Building a Tool to Calculate Age from Name Preferences.”
Today I reopened a slightly neglected project I started a few weeks ago. The goal is to build a web app that presents several names and asks users to pick their favorite. After a number of ranking cycles, the app will calculate the year in American history where the general population had name sensibilities most similar to the user. Right now it runs as a command line application, but it will soon become a Flask web app.
Most recently, I added a few lines that prevented the random number generator from selecting the same name index twice and asking users to compare a name to itself. Here is the name ranking code in it’s current form. The whole project is now on my GitLab.
#Enter name ranking cycle
while True:
#Generate two name index integers
while True:
name_1_index = np.random.randint(0,len(unique_names))
name_2_index = np.random.randint(0,len(unique_names))
if name_1_index != name_2_index:
break
#Get names corresponding to those indices
name_1 = unique_names.iloc[name_1_index]
name_2 = unique_names.iloc[name_2_index]
#Present names for the user to choose
print("Which name do you prefer?\n")
print(f'{name_1[0]}---1---2---3---4---5---{name_2[0]}')
#Take user's ranking from 1 to 5
preference = input()
#Assign points based on name preference
if preference == '1':
#Reward preferred name with +2 points and penalize other name with -2 points
unique_names.iloc[name_1_index,1] += 2
unique_names.iloc[name_2_index,1] -= 2
elif preference == '2':
#Reward preferred name with +1 point and penalize other name with -1 point
unique_names.iloc[name_1_index,1] += 1
unique_names.iloc[name_2_index,1] -= 1
elif preference == '3':
#Do nothing, because neither name is preferred
pass
elif preference == '4':
#Reward preferred name with +1 point and penalize other name with -1 point
unique_names.iloc[name_1_index,1] -= 1
unique_names.iloc[name_2_index,1] += 1
elif preference == '5':
#Reward preferred name with +2 points and penalize other name with -2 points
unique_names.iloc[name_1_index,1] -= 2
unique_names.iloc[name_2_index,1] += 2
elif preference == 'v':
#Print the current state of the ranking table
print(unique_names.sort_values(by='preference_score'))
print()
elif preference == 'x':
break
else:
print('***INVALID INPUT***')