0

私はしばらくの間このPythonの問題に取り組んできました。私はイントロレベルのクラスにいて、すっごく立ち往生しています。現在、エラーは発生していませんが、プログラムが(names.txtからの)データを出力していないか、検索を求められていません。どんな助けでもいただければ幸いです。-ありがとう!

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()
4

2 に答える 2

0

main()あまり効果はありません。数行だけ印刷します。他の関数を呼び出させたいと思うでしょう。

于 2012-11-20T02:37:03.943 に答える
0

変更を最小限に抑えた修正版:

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()

変更点:

  • get_names では、読み取られた name_list は実際にはリストではなく文字列でした。行のリストを取得するには、改行 ( ) で分割する必要があります.split('\n')。その後、名前から改行を削除する必要はありません

  • get_names() が戻った後、リストを保存して他の関数に渡す必要があります

  • new_file は name_list を使用しましたが、引数として受け取りませんでした

  • try/except ブロックは while ブロックにネストする必要があります

おめでとうございます、うまくいっているようです (:

于 2012-11-20T03:11:49.453 に答える