else块无法正常工作

时间:2016-12-25 19:26:45

标签: python if-statement

我尝试在python中创建一个书单。

以下是样本列表:

该列表包含book_id,bookName,writerName,writerSurname,cost对象。

  books = [
     [45623, 'Ptyhon', 'Mustafa', 'Basak', 23],
     [99878, 'Linux Networks', 'Mustafa', 'Basak', 26],
     [98938, 'Operating Systems', 'Ali', 'Akinci', 17],
     [98947, 'PHP and AJAX', 'Haydar', 'Baskan', 25]
     ]

我想根据作家姓氏搜索列表。

 while 1:
     writerSurname = input('Pls enter the writer's surname.')
     if writerSurname not in ['exit', 'Exit']:
         for k in books:
             if str(writerSurname) == k[3]:
                 print(k[1],'writer', k[2],k[3], "cost is", k[4],"TL")

     else:
         print(writerSurname, 'there is no such a person.')
         break

但是其他块无法正常工作。当我输入一个不在图书清单中的姓氏时,它不显示打印(writerSurname,'没有这样的人。')行。有人可以告诉我哪里犯了错误吗?

5 个答案:

答案 0 :(得分:1)

除了无效的语法之外,if语句不关心姓氏是否在书籍列表中:您唯一要检查的是一种类型是否退出/退出。

答案 1 :(得分:1)

对于不在图书清单中的任何输入,当输入不是“退出”或“退出”时,代码进入if块。

这是正确的版本:

books = [
    [45623, 'Ptyhon', 'Mustafa', 'Basak', 23],
    [99878, 'Linux Networks', 'Mustafa', 'Basak', 26],
    [98938, 'Operating Systems', 'Ali', 'Akinci', 17],
    [98947, 'PHP and AJAX', 'Haydar', 'Baskan', 25]
]

while 1:
    writerSurname = raw_input('Pls enter the writers surname.')
    if writerSurname not in ['exit', 'Exit']:
        found = False
        for k in books:
            if str(writerSurname) == k[3]:
                print(k[1],'writer', k[2],k[3], "cost is", k[4],"TL")
                found = True
        if not found:
            print(writerSurname, 'there is no such a person.')
    else:
        break

答案 2 :(得分:1)

如果某个名称匹配

,内部循环应使用标志集
while True:
     writerSurname = input("Pls enter the writer's surname.")
     if writerSurname in ['exit', 'Exit']:
         break
     found = False
     for k in books:
         if writerSurname == k[3]:
             found = True
             print(k[1],'writer', k[2],k[3], "cost is", k[4],"TL")
     if not found:
         print(writerSurname, 'there is no such a person.')
         break

请注意,对于单个中断匹配(此处不适用),您可以使用for/else语句:

 for k in books:
     if writerSurname == k[3]:
         print(k[1],'writer', k[2],k[3], "cost is", k[4],"TL")
         break
 else:
     # end of the loop reached, without break: enters here
     print(writerSurname, 'there is no such a person.')

答案 3 :(得分:1)

您有缩进错误和一些逻辑错误。您的代码中的所有混乱都不是必需的,因此我将代码简化为以下内容:

books = [
     [45623, 'Ptyhon', 'Mustafa', 'Basak', 23],
     [99878, 'Linux Networks', 'Mustafa', 'Basak', 26],
     [98938, 'Operating Systems', 'Ali', 'Akinci', 17],
     [98947, 'PHP and AJAX', 'Haydar', 'Baskan', 25]
     ]

while True: 
    surname = input("pls enter the writer's surname: ") 
    for record in books:
        if surname in record:
            print("..details..")
            break 
    else: 
        print("Failed")

注意:如果您正在处理详细信息/记录,如姓名,号码和这些详细信息相关,并且您认为您将不断搜索这些详细信息,我发现使用词典更加方便,速度更快而不是在列表中线性搜索。

while循环允许您编程多个条目,因此您可以为程序输入无限数量的名称。内部for循环完成实际工作,在嵌入式列表中搜索以查找姓氏(如果存在)。如果找到姓氏,将执行print("...details..."),如果搜索了所有嵌入列表但未找到匹配的姓氏,则将执行print("Failed")

希望这对你有所帮助并祝你好运!

答案 4 :(得分:0)

试试这个:

books = [
     [45623, 'Ptyhon', 'Mustafa', 'Basak', 23],
     [99878, 'Linux Networks', 'Mustafa', 'Basak', 26],
     [98938, 'Operating Systems', 'Ali', 'Akinci', 17],
     [98947, 'PHP and AJAX', 'Haydar', 'Baskan', 25]
 ]

surnames = [book[3].lower() for book in books]

while 1:
    writerSurname = input("Pls enter the writer's surname.").lower()
    if writerSurname in surnames:
        for _, book_name, author_name, author_surname, cost in books:
            if author_surname.lower() == writerSurname:
                print("{book_name} writer {author_name} {author_surname} cost is {cost} TL".format(book_name=book_name, author_name=author_name, author_surname=author_surname, cost=cost))
    else:
        print(writerSurname, 'there is no such a person.')
        if writerSurname == 'exit':
            break