【问题标题】:Adding in "else" makes my for loop not work添加“else”使我的for循环不起作用
【发布时间】:2019-11-30 22:06:43
【问题描述】:

我是编码新手,我有一个需要搜索的列表列表。 我想查看较大列表中包含的哪些列表将变量 full_choice 作为序列中的第三项。 所有包含 third_choice 的列表我都需要打印到 txt 文件中。

下面的代码可以正常工作并将我需要的内容添加到文件中,但是如果变量 full_choice 不匹配,我需要重新启动该函数。

def display_instructor_txt():
    file_name = input('type in the name of the file you want to create do not include .txt')
    file_name_full = file_name + '.txt'
    new_file = open(file_name_full,'w')
    first_choice = input('type in the first name of the instructor you want to filter by ')
    last_choice = input('type in the last name of the instructor you want to filter by ')
    full_choice = first_choice[0].upper() + first_choice[1:].lower() + last_choice[0].upper() + last_choice[1:].lower()
    for course in all_courses_list:
        if course[2].replace(" ","").replace(",","") == full_choice:
            course_st = ''.join(course)
            new_file.write(course_st.replace('[','').replace(']','').replace("'",'').replace('\\n','').replace(" ", ", "))
    else:
        print('please try again')
        display_instructor_txt()


我尝试在代码末尾插入 else: ,但是虽然这最终会创建文件,但它不会向其中写入任何内容。

【问题讨论】:

  • else之后到底写了什么代码?
  • 请显示无效的代码。
  • else: print('name not found please try again') dispaly_instructor_txt()
  • @AndreaRichmond:编辑问题并将其添加到那里。在 python 中,缩进很重要,问题中的代码示例与导致错误的原因完全相同,这一点非常重要。
  • 代码中的'''''''''''' 应该是做什么用的?那缩进是否正确?

标签: python python-3.x for-loop if-statement


【解决方案1】:

正如@Haken Lid 所怀疑的,请修复缩进:

for course in all_courses_list:
    if course[2].replace(" ","").replace(",","") == full_choice:
        course_st = ''.join(course)
        new_file.write(course_st.replace('[','').replace(']','').
                       replace("'",'').replace('\\n','').replace(" ", ", "))
    else:
        print('please try again')
        display_instructor_txt()

【讨论】:

  • 我将缩进更改为您所做的,但它仍然不适合我,如果我删除 else 行它可以工作
  • 请检查 print(course[2].replace(" ","").replace(",",""))print(full_choice) 返回什么?
  • 他们在 if course[2].replace(" ","").replace(",","") == full_choice 之后打印相同的内容:难道是 for循环遍历列表,它最初发现它们不相等,因此触发 else。
【解决方案2】:

尝试修复您的缩进。我猜你想要这样的东西:

def display_instructor_txt():
    file_name = input('type in the name of the file you want to create do not include .txt')
    file_name_full = file_name + '.txt'
    new_file = open(file_name_full,'w')
    first_choice = input('type in the first name of the instructor you want to filter by ')
    last_choice = input('type in the last name of the instructor you want to filter by ')
    full_choice = first_choice[0].upper() + first_choice[1:].lower() + last_choice[0].upper() + last_choice[1:].lower()
    for course in all_courses_list:
        if course[2].replace(" ","").replace(",","") == full_choice:
            course_st = ''.join(course)
            new_file.write(course_st.replace('[','').replace(']','').replace("'",'').replace('\\n','').replace(" ", ", "))
        else:
            print('please try again')
            display_instructor_txt()

我刚刚将 else 块向前移动以与您之前几行的 if 块对齐。

【讨论】:

  • 感谢您的回复,我刚刚尝试过,但它仍然不起作用,如果我完全摆脱 else 它确实有效....
  • @AndreaRichmond 也许将else 编辑为更直接的内容?喜欢else if . . . 效果会更好。
猜你喜欢
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-13
相关资源
最近更新 更多