有问题退出我的while循环

时间:2014-11-15 22:25:30

标签: python python-3.x

过去几个小时我一直在测试我的代码而且我很难过。该程序采用名称的文本文件,将名称转换为列表,打印名称,对名称进行排序,打印排序的名称,然后允许您搜索列表。一切似乎都运行良好,但我遇到的一个问题是退出while循环。如果选择y或Y,则可以再次搜索,但如果选择了其他任何内容,也会发生这种情况。我在循环外添加了一个print语句,所以如果选择y以外的任何东西,那么程序应该以最后打印的字符串结束,但它似乎不起作用。有没有人对它为什么不起作用以及我可以改变什么来让它起作用?

感谢您的时间。

#define the main function
def main():

    #create a variable to control the loop
    keep_going = 'y'

    #setup loop to search for name
    while keep_going == 'y' or keep_going == 'Y':    

        #call input name function
        names = input_name()

        #call print name function
        print_name(names)

        #sort the printed list
        names.sort()

        #call the print name function
        print_name(names)

        #call the output name function
        output_name(names)

        #call the search name function
        search_name(names)

        #add user input for another search
        search_again = input('Would you like to make another search?(y for yes): ')   

    #print if anything other than y or Y is selected
    print()
    print('Goodbye!')

#define the input function   
def input_name():

    #open the names.txt file
    infile = open('names.txt', 'r')

    #read contents into a list
    names = infile.readlines()

    #close the file
    infile.close()

    #strip the \n from each element
    index = 0
    while index < len(names):
        names[index] = names[index].rstrip('\n')
        index += 1

    #return the list back to main function    
    return names

#define the print name function
def print_name(names):

    #print the contents of the list
    for name in names:
        print(name)

#define the output name function
def output_name(names):

    #open file for writing
    outfile = open('sorted_names.txt', 'w')

    #write the list to the file
    for item in names:
        outfile.write(item + '\n')

    #close the file
    outfile.close()

    #return to main function
    return

#define the search name function
def search_name(names):

    #add a user input to search the file
    search = input('Enter a name: ')

    #determine whether the name is in the list
    if search in names:

        #get the names index
        name_index = names.index(search)

        #print the name was found and give the items index
        print(search, "was found in list. This item's index is", name_index)         

    else:

        #print the item was not found
        print(search, 'was not found in the list.')    

main() 

3 个答案:

答案 0 :(得分:0)

您正在测试keep_going,但设置search_again

答案 1 :(得分:0)

#create a variable to control the loop
keep_going = 'y'

#setup loop to search for name
while keep_going == 'y' or keep_going == 'Y':

    #add user input for another search
    search_again = input('Would you like to make another search?(y for yes): ')

您永远不会将keep_going设置为其他内容。相反,您要求用户输入y以继续,但将其存储在search_again中(然后将其丢弃)。您需要更改它以将值存储在keep_going

答案 2 :(得分:0)

替换第29行,其中说明:

search_again = input('Would you like to make another search?(y for yes): ')

以下内容:

keep_going = input('Would you like to make another search?(y for yes): ')

因为当您输入yY时,您正在为该字母定义变量search_again而不是变量keep_going,而变量要求循环停止