Skip to content

Instantly share code, notes, and snippets.

@davegotz
Created February 14, 2019 15:48
Show Gist options
  • Save davegotz/3ca036e899be6681e69eb29954ff9aa9 to your computer and use it in GitHub Desktop.
Save davegotz/3ca036e899be6681e69eb29954ff9aa9 to your computer and use it in GitHub Desktop.
Determines the popularity of a name.
# Determines the popularity rank for name_to_fine and returns that
# as an integer.
def get_name_rank(name_to_find, name_file):
line = name_file.readline().rstrip('\n')
count = 1
while (line != name_to_find) and (line != ''):
# Read the next line.
line = name_file.readline().rstrip('\n')
count += 1
if line == '':
return -1
else:
return count
# Determines the popularity rank for the name Waldo.
def main():
# Get the name to find.
name_to_find = input("What name do you wish to find (IN ALL CAPS): ")
# Open the file.
name_file = open("census-first-names.txt", "r")
# Find the rank for the name to find
name_rank = get_name_rank(name_to_find, name_file)
# Print out the rank.
if name_rank < 0:
print(name_to_find,"was not found.")
else:
print(name_to_find+"'s rank is: ", name_rank)
# Close the file
name_file.close()
# Run the program.
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment