+ 1

Can someone help me with my code?

Write a program that reads the contents of the two files into two separate lists. The program displays a menu of two options: first option allows the user to enter a letter and the program should prints lists of girls and boys names that begin with that letter. Second option: allows the user to enter a length of a name and the program should prints lists of girls and boys names that have the same length. girlNamesfile = open("GirlNames.txt", "r") girlNamesList = [] girlNames = girlNamesfile.readline() while girlNames != "": girlNamesList.append(girlNames) girlNames = girlNamesfile.readline() print(girlNamesList) boyNamesfile = open("BoyNames.txt", "r") boyNamesList = [] boyNames = boyNamesfile.readline() while boyNames != "": boyNamesList.append(boyNames) boyNames = boyNamesfile.readline() print(boyNamesList)

2nd Nov 2020, 4:16 PM
...
... - avatar
5 Réponses
+ 1
I dont have a complete answer, but, instead of using the readline() and appending the line in a list, file objects have a method called readlines() which returns a list of all the lines in the file. I'll edit this with more info after work
2nd Nov 2020, 4:42 PM
LDSD
+ 1
1) Ok so, for your option, you can obtain the names with: names_iter = filter(lambda name: name[0] == letter, girlNamesList + boyNamesList) filter returns a filter object, which is an iterable. You can convert it to list(or tuple), or print them out like: print(*names_iter, sep='\n') 2) Using the same approach as option 1 names_iter = filter(lambda name: len(name) == length, girlNamesList + boyNamesList) If you want to read more on filter, (and also map): https://stackabuse.com/map-filter-and-reduce-in-JUMP_LINK__&&__python__&&__JUMP_LINK-with-examples/
2nd Nov 2020, 8:33 PM
LDSD
0
@LDSD thank you so much
2nd Nov 2020, 4:46 PM
...
... - avatar
0
updated: girlNamesfile = open("GirlNames.txt", "r") girlNamesList = [] girlNames = girlNamesfile.readline() while girlNames != "": girlNamesList.append(girlNames) girlNames = girlNamesfile.readline() print(girlNamesList) boyNamesfile = open("BoyNames.txt", "r") boyNamesList = [] boyNames = boyNamesfile.readline() while boyNames != "": boyNamesList.append(boyNames) boyNames = boyNamesfile.readline() print(boyNamesList) int(input("Enter 1 for list of names begin with a letter"\n"Enter 2 for list of names that have the same length")) if option ==1: letter= input("Enter a letter" print(letter)) print("list of boy names begin with" letter) print(letter.find) else
: length =int(input("Enter a length")) print(length)
2nd Nov 2020, 5:33 PM
...
... - avatar
0
LDSD thank you for your explanation and help
3rd Nov 2020, 2:32 AM
...
... - avatar