Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Popular Name with Functions To demonstrate how functions can be used to reduce redundant code in a program and improve the logic flow, you are

Popular Name with Functions

To demonstrate how functions can be used to reduce redundant code in a program and improve the logic flow, you are going to rewrite Programming Assignment 9 Popular Names using functions. The new program must use at least two new functions:

The get_names_list function will take in a filename as a parameter and return a list of the names from the file. This function will be called to read the girl names file and create the girl names list and called again to read the boy names file and create the boy names list. The function header statement should look like:

def get_names_list(filename):

The check_name function will take in two parameters; a name, and a list, and return the rank of the name in the list. If the name is not in the list, it will return a rank of zero. This function will be called to check a name in either the boy names list or the girl names list. The function header statement should look like:

def check_name(name, name_list):

The functions should not contain any input or print statements.

The main logic of the program will be modified to use these two functions.

*** Note: I am able to get most of the program working but cannot seem to find a way to loop the program without the parameters getting screwed up after the first loop through.

MY VERSION OF CODE SO FAR:

def get_names_list(filename): with open(filename, "r") as filename: file_list = [] for line in filename: data = line line = str(data.strip()) file_list.append(line) return file_list def check_name(name, file_list): rank = 0 for i in range(0, len(file_list)): if file_list[i].lower() == name_input.lower(): rank = i + 1 break return rank boy_list = get_names_list("BoyNames.txt") girl_list = get_names_list("GirlNames.txt") name_input = input(str("Enter a Name: ")) girl_rank = check_name(name_input, girl_list) boy_rank = check_name(name_input, boy_list) while name_input != "Stop": if boy_rank != 0 and girl_rank != 0: print(name_input, "is a popular girls name and is ranked:", girl_rank) print(name_input, "is a popular boys name and is ranked:", boy_rank) print() elif boy_rank != 0: print(name_input, "is a popular boys name and is ranked:", boy_rank) print(name_input, "is not a popular girls name.") print() name_input = input(str("Enter a Name: ")) elif girl_rank != 0: print(name_input, "is a popular girls name and is ranked:", girl_rank) print(name_input, "is not a popular boys name.") print() name_input = input(str("Enter a Name: ")) else: print(name_input, "is not a popular girls name.") print(name_input, "is not a popular boys name.") print() name_input = input(str("Enter a Name: ")) else: print("Stopping Program.")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

BoyNames.txt

Jacob Michael Joshua Matthew Daniel Christopher Andrew Ethan Joseph William Anthony David Alexander Nicholas Ryan Tyler James John Jonathan Noah Brandon Christian Dylan Samuel Benjamin Zachary Nathan Logan Justin Gabriel Jose Austin Kevin Elijah Caleb Robert Thomas Jordan Cameron Jack Hunter Jackson Angel Isaiah Evan Isaac Mason Luke Jason Gavin Jayden Aaron Connor Aiden Aidan Kyle Juan Charles Luis Adam Lucas Brian Eric Adrian Nathaniel Sean Alex Carlos Bryan Ian Owen Jesus Landon Julian Chase Cole Diego Jeremiah Steven Sebastian Xavier Timothy Carter Wyatt Brayden Blake Hayden Devin Cody Richard Seth Dominic Jaden Antonio Miguel Liam Patrick Carson Jesse Tristan Alejandro Henry Victor Trevor Bryce Jake Riley Colin Jared Jeremy Mark Caden Garrett Parker Marcus Vincent Kaleb Kaden Brady Colton Kenneth Joel Oscar Josiah Jorge Cooper Ashton Tanner Eduardo Paul Edward Ivan Preston Maxwell Alan Levi Stephen Grant Nicolas Omar Dakota Alexis George Collin Eli Spencer Gage Max Cristian Ricardo Derek Micah Brody Francisco Nolan Ayden Dalton Shane Peter Damian Jeffrey Brendan Travis Fernando Peyton Conner Andres Javier Giovanni Shawn Braden Jonah Cesar Bradley Emmanuel Manuel Edgar Erik Mario Edwin Johnathan Devon Erick Wesley Oliver Trenton Hector Malachi Jalen Raymond Gregory Abraham Elias Leonardo Sergio Donovan Colby Marco Bryson Martin

GirlNames.txt

Emily Madison Emma Olivia Hannah Abigail Isabella Samantha Elizabeth Ashley Alexis Sarah Sophia Alyssa Grace Ava Taylor Brianna Lauren Chloe Natalie Kayla Jessica Anna Victoria Mia Hailey Sydney Jasmine Julia Morgan Destiny Rachel Ella Kaitlyn Megan Katherine Savannah Jennifer Alexandra Allison Haley Maria Kaylee Lily Makayla Brooke Mackenzie Nicole Addison Stephanie Lillian Andrea Zoe Faith Kimberly Madeline Alexa Katelyn Gabriella Gabrielle Trinity Amanda Kylie Mary Paige Riley Jenna Leah Sara Rebecca Michelle Sofia Vanessa Jordan Angelina Caroline Avery Audrey Evelyn Maya Claire Autumn Jocelyn Ariana Nevaeh Arianna Jada Bailey Brooklyn Aaliyah Amber Isabel Danielle Mariah Melanie Sierra Erin Molly Amelia Isabelle Madelyn Melissa Jacqueline Marissa Shelby Angela Leslie Katie Jade Catherine Diana Aubrey Mya Amy Briana Sophie Gabriela Breanna Gianna Kennedy Gracie Peyton Adriana Christina Courtney Daniela Kathryn Lydia Valeria Layla Alexandria Natalia Angel Laura Charlotte Margaret Cheyenne Mikayla Miranda Naomi Kelsey Payton Ana Alicia Jillian Daisy Mckenzie Ashlyn Caitlin Sabrina Summer Ruby Rylee Valerie Skylar Lindsey Kelly Genesis Zoey Eva Sadie Alexia Cassidy Kylee Kendall Jordyn Kate Jayla Karen Tiffany Cassandra Juliana Reagan Caitlyn Giselle Serenity Alondra Lucy Kiara Bianca Crystal Erica Angelica Hope Chelsea Alana Liliana Brittany Camila Makenzie

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions