def male(year, rank): ''' This function prints some statistics about the male name of an inputed rank, rank in an specific, inputed year, year. Statistics include the number of people with that name that year, the total names in the top 250 list that year, and the percentage of children in the list with the given name. Also printed is a histogram of '*'s for the inputed year, a year prior, and a year after where each '*' corresponds to 0.1 of a percent. ''' read_names.read_from_file("top_names_1880_to_2014.txt") ##finds name of a given rank and year, defines that name in variable "name" (male_names, male_counts) = read_names.top_in_year(year, 'M') print("The rank {} most popular male name in {} is {}".format( rank, year, male_names[rank - 1])) print("\t{} out of {} or {:.2f}%".format( male_counts[rank - 1], sum(read_names.all_male_counts[year - 1880]), (male_counts[rank - 1] / sum(read_names.all_male_counts[year - 1880]) * 100))) print("Histogram for {}".format(male_names[rank - 1])) name = male_names[rank - 1] ##redefines male_names and male_counts to the year before the inputed year, finds the index (position) of the targeted name, finds the percent of the name used in that year, calculates the number of asterisks, prints histogram for that year (male_names, male_counts) = read_names.top_in_year((year - 1), 'M') ##if statement for if name does not appear in the list for a year if male_names.count(name) == 0: percent = 0 else: position = read_names.all_male_names[(year - 1) - 1880].index(name) percent = male_counts[position] / sum( read_names.all_male_counts[(year - 1) - 1880]) * 100 ast = (int(percent * 10)) * '*' print("{}:\t{}\t({:.2f}%)".format(year - 1, ast, percent)) ##redefines male_names and male_counts to the inputed year, finds the index (position) of the targeted name, finds the percent of the name used in that year, calculates the number of asterisks, prints histogram for that year (male_names, male_counts) = read_names.top_in_year(year, 'M') position = read_names.all_male_names[year - 1880].index(name) percent = male_counts[position] / sum( read_names.all_male_counts[year - 1880]) * 100 ast = (int(percent * 10)) * '*' print("{}:\t{}\t({:.2f}%)".format(year, ast, percent)) ##redefines male_names and male_counts to the year after the inputed year, finds the index (position) of the targeted name, finds the percent of the name used in that year, calculates the number of asterisks, prints histogram for that year (male_names, male_counts) = read_names.top_in_year((year + 1), 'M') ##if statement for if name does not appear in the list for a year if male_names.count(name) == 0: percent = 0 else: position = read_names.all_male_names[(year + 1) - 1880].index(name) percent = male_counts[position] / sum( read_names.all_male_counts[(year + 1) - 1880]) * 100 ast = (int(percent * 10)) * '*' print("{}:\t{}\t({:.2f}%)".format(year + 1, ast, percent))
index = names.index(name) rank = index + 1 count = counts[index] percentage1 = (count / counts[0]) * 100 percentage2 = (count / sum(counts)) * 100 outstring = " " + str( year) + ": {:3d} {:5d} {:7.3f} {:7.3f}".format( rank, count, percentage1, percentage2) print(outstring) else: # Name not found print(" ", year, ": Not in the top 250", sep="") #main body read_names.read_from_file("top_names_1880_to_2014.txt") y = int(input("Enter the year to check => ")) print(y) if y < 1880 or y > 2014: #Checks for valid date print("Year must be at least 1880 and at most 2014") sys.exit() #Ladies first name = input("Enter a female name => ") print(name) print("Data about female names") print(name, ":", sep="")
name_1 = list_name[position - 2] name_2 = list_name[position - 1] name_3 = list_name[position] name_4 = list_name[position + 1] name_5 = list_name[position + 2] print table(name_1, list_name, list_counts) print table(name_2, list_name, list_counts) print table(name_3, list_name, list_counts) print table(name_4, list_name, list_counts) print table(name_5, list_name, list_counts) else: print '%s is not in top 250.' % name # read in year and names from the user and call the functions above to output the table required. read_names.read_from_file('top_names_1880_to_2014.txt') year = raw_input('Enter the year to check => ') print year if int(year) < 1880 or int(year) > 2014: print 'Year must be at least 1880 and at most 2014' sys.exit() print female_name = raw_input('Enter a female name => ') print female_name print 'Data about female names' (female_names, female_counts) = read_names.top_in_year(int(year), 'f') print 'Top ranked name %s' % female_names[0] print '250th ranked name %s' % female_names[249] list_250_female = female_names[0:250] female = table_(female_name, female_names, female_counts)