(use Python please )older are two text files: boys.txt and girls.txt. Use these
ID: 3888801 • Letter: #
Question
(use Python please )older are two text files: boys.txt and girls.txt. Use these files to write a program to do the following:Create a dictionary from the boys.txt file that uses the boys’ names as the key and the numberof boys that were named that name as the value. The names and counts are delimited by a comma.Create a dictionary from the girls.txt file that uses the girls’ names as the key and the number of girls that were named that name as the value. The names and counts are delimited by a comma.Create a menu that contains the following choices:oAdd a name and count for either a boy or girloDelete a name and count for either a boy or girloFind the count for a particular boy name or girl nameoFind the boy name with the highest countoFind the girl name with the highest countoQuit the programUse methods where appropriate. Use descriptive identifier names. Use comments to describe each method.BE SURE TO USE DICTIONARIES AS THE ONLY MEANS TO COMPLETE THE MENU CHOICES. DO NOT USE LISTS OR ANY OTHER DATA TYPE OR DATA STRUCTURE TO SOLVE THIS PROBLEM.
Explanation / Answer
#!usr/bin/python
boy_dict = {}
file1 = open("boys.txt","r")
for line in file1:
boy_dict[line.split(',')[0]] = int(line.split(',')[1])
file1.close()
girl_dict = {}
file1 = open("girls.txt","r")
for line in file1:
girl_dict[line.split(',')[0]] = int(line.split(',')[1])
file1.close()
print(boy_dict)
print(girl_dict)
choice = 0
while choice != 5:
print("1.Add name and count for girl or boy")
print("2.Delete name and count for girl or boy")
print("3.Find boy name with highest count")
print("4.Find girl name with highest count")
print("5.Quit ")
choice = int(input("Enter choice :"))
if choice == 1:
num = int(input("Enter 1 for boy and 2 for girl :"))
name = input("Enter name :")
count = int(input("Enter count :"))
if num == 1:
boy_dict[name] = count
if num == 2:
girl_dict[name] = count
if choice == 2:
num = int(input("Enter 1 for boy and 2 for girl :"))
name = input("Enter name :")
if num == 1:
if name in boy_dict:
del boy_dict[name]
else:
print("Name not found")
if num == 2:
if name in girl_dict:
del girl_dict[name]
else:
print("Name not found")
if choice == 3:
max = 0
name = ''
for key in boy_dict.keys():
if boy_dict[key] > max :
max = int(boy_dict[key])
name = key
print(name, max)
if choice == 4:
max = 0
name = ''
for key in girl_dict.keys():
if girl_dict[key] > max :
max = girl_dict[key]
name = key
print(name, max)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.