Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:26:16+00:00 2026-06-14T14:26:16+00:00

I have been working on this python issue for a while. I am in

  • 0

I have been working on this python issue for a while. I am in an intro level class and I am soooo stuck. Right now I am getting no errors but the program is not printing the data (from names.txt) or prompting me to search. any help would be appreciated. -thanks!

def main():

    print("Last, \tFirst")
    print

    name_list = get_names()
    print_list(name_list)
    new_file(name_list)
    search_list(name_list)

def get_names():
    # open the data file
    infile = open('names.txt', 'r')

    # read all the lines from the file
    name_list = infile.read().split('\n')

    #close the input file
    infile.close()



    return(name_list)

#def print list
def print_list(name_list):
    #print the data
    for name in name_list:
        print (name)
    return(name)

#def new_file
def new_file(name_list):
    outfile = open('sorted_names.txt' ,'w')
    for item in name_list:
        outfile.write(item + '\n')

    outfile.close()

#def search_list
def search_list(name_list):
    again = 'Y'
    while again == 'Y':
        name = input("What name would you like to look for? :")
        try:
            name_index = name_list.index(name)
            print (name), (" was found in the list at index point: "), name_index

        except ValueError as err:
            print (name), (" was not found in the list.")

            print ("Would you like to search for another name?")
            again = input("Would you like to run the program again? [y/n]") == 'y'


# execute the main function
main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:26:17+00:00Added an answer on June 14, 2026 at 2:26 pm

    Corrected version with minimum changes:

    def main():
    
        print("Last, \tFirst")
        print
    
        name_list = get_names()
        print_list(name_list)
        new_file(name_list)
        search_list(name_list)
    
    def get_names():
        # open the data file
        infile = open('names.txt', 'r')
    
        # read all the lines from the file
        name_list = infile.read().split('\n')
    
        #close the input file
        infile.close()
    
        #print data read into memory
        print(name_list)
    
        return(name_list)
    
    #def print list
    def print_list(name_list):
        #print the data
        for name in name_list:
            print (name)
        return(name)
    
    #def new_file
    def new_file(name_list):
        outfile = open('sorted_names.txt' ,'w')
        for item in name_list:
            outfile.write(item + '\n')
    
        outfile.close()
    
    #def search_list
    def search_list(name_list):
        again = 'Y'
        while again.upper()== 'Y':
            name = raw_input("What name would you like to look for? :")
            try:
                name_index = name_list.index(name)
                print (name), (" was found in the list at index point: "), name_index
    
            except ValueError as err:
                print (name), (" was not found in the list.")
    
                print ("Would you like to search for another name?")
                again = raw_input("Would you like to run the program again? [y/n]") == 'y'
    
    
    # execute the main function
    main()
    

    What changed:

    • in get_names, the name_list read was actually a string, not a list. To get the list of lines you need to split on the newlines (.split('\n')). After that you don’t need to remove the newlines from the names

    • after get_names() returns, you need to store the list and pass to the other functions

    • new_file used name_list, but didn’t receive it as argument

    • the try/except block should be nested in the while block

    Congrats, it seems to be working (:

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this problem for a while now. I am trying
I have been working with python for a while now. Recently I got into
I have been working on and off with Java/Python. Now in this situation I
I have been working on this for few hours and got stuck, so how
I have been working on this problem for 2 days now and it's an
I have been working on this for 24 hours now, trying to optimize it.
I have been working on this for some time now and just cant quite
I have been working on a huge project for work for a while now,
I have been working on getting a python CGI script to work all day,
I have been working on Python The Hard Way and am getting the above

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.