0

I want to display the position of the name being searched for in a file. I know that I am missing something or maybe have named something incorrectly.

I am very new to Python and programming in general so please bear with me. Please explain what I have missed or done incorrectly so I may complete this program. Thanks for all help in advance.

try:
     boyfile = open("boynames2014.txt", "r")
     girlfile = open("girlnames2014.txt", "r")

 except IOError:
     print("Error: file not found")



 gender = input("Enter gender (boy/girl): ")
 if gender == "boy" or gender == "girl":
     name = (input("Enter name to search for: "))
 else:
     print("Invalid gender")

 if name in boyfile:
     pos = boyfile.index(name)
     print(name, "was ranked #", pos, "in 2014 for boy names")

 else:
     print(name, "was not ranked in the top 100 boy names for 2014") 

 if name in girlfile:
     pos = girlfile.index(name)
     print(name, "was ranked #", pos, "in 2014 for girl names")

 else:
     print(name, "was not ranked in the top 100 girl names for 2014")




 boyfile.close()
 girlfile.close()
  • You can check what's in your `boyfile` and `girlfile`. – niraj Mar 20 '17 at 22:57
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. Since you have failed to give us the input you're using or the output you got, there's not a lot we can do to help at this point. – Prune Mar 20 '17 at 22:58

2 Answers2

0

When you are trying

if name in boyfile:

or the same for girlfile, you are trying to check for name variable name in an IOWrapper kind of element, it is not the list containing the file contents, it is just a reference to the file object here. you need to read the contents to a list

try:
    boyfile = open("boynames2014.txt", "r")
    girlfile = open("girlnames2014.txt", "r")
    boyname_list = [line.strip() for line in boyfile]   #read the content to a list
    girlname_list = [line.strip() for line in girlfile] #read the content to a list

except IOError:
    print("Error: file not found")



gender = input("Enter gender (boy/girl): ")
if gender == "boy" or gender == "girl":
    name = (input("Enter name to search for: "))
    if gender == "boy":
        search_list = boyname_list
    else:
        search_list = girlname_list
    if name in search_list:
        pos = search_list.index(name)
        print(name, "was ranked #", pos, "in 2014 for", gender," names")
    else:
        print(name, "was not ranked in the top 100", gender," names for 2014")
else:
    print("Invalid gender")


boyfile.close()
girlfile.close()
Mohit
  • 891
  • 10
  • 25
  • This is good, but it also will print, for example if I used a boy name, "Boyname was not ranked in the top 100 girl names for 2014". Same thing if using a girl name. Is there a way to keep it from doing that? –  Mar 20 '17 at 23:54
  • I have updated the code, so that becomes easy for you. – Mohit Mar 21 '17 at 07:54
0

You can try following to read the file and build list for boyfile and girlfile: For reference you can view link for reading file:

try:
    with open("boynames2014.txt", "r") as f:
        boyfile = []
        for line in f:
            boyfile.append(line.strip())

    with open("girlnames2014.txt", "r") as f:
        girlfile = []
        for line in f:
            girlfile.append(line.strip())

except IOError:
     print("Error: file not found")

Or, you can also use following as referred in here:

try:
    boyfile = [line.strip() for line in open('boynames2014.txt')]
    girlfile = [line.strip() for line in open('girlnames2014.txt')]

except IOError:
    print("Error: file not found")
Community
  • 1
  • 1
niraj
  • 17,498
  • 4
  • 33
  • 48