You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python笔记管理器删除功能求助:如何在展示前移除空笔记

解决笔记管理器的删除问题与空笔记过滤

一、修复del函数误删问题

你之前用del时出现误删,核心原因是删除后列表长度和索引会变化,但用户输入的序号还是基于删除前的列表。比如删除第一个笔记后,原来的第二个笔记会变成新的第一个,此时用户再输入原序号就会删错。

修复方法很简单:每次删除前先展示当前最新的笔记列表,让用户基于当前序号输入,同时用del直接移除元素:

修改delete_note方法

def delete_note(self, note_index):
    if 0 <= note_index < len(self.notes):
        # 直接删除对应索引的元素,而非设为空字符串
        del self.notes[note_index]
    else:
        print('ERROR: Invalid note index.')

修改删除流程(每次删除前显示当前笔记)

if d_note == 'yes':
    try:
        notes_d = int(input('How many notes to delete?: '))
        for _ in range(notes_d):
            # 每次删除前先展示当前所有笔记,让用户明确序号
            notes.display_notes()
            w_note = int(input('Which note to delete?: '))
            if 1 <= w_note <= len(notes.notes):
                notes.delete_note(w_note - 1)
            else:
                print('ERROR: Invalid note index.')
    except ValueError:
        print('ERROR: Invalid input for note index.')

二、如果坚持用空字符串标记删除,过滤空笔记后展示

如果你想保留设空字符串的方式,只需修改display_notes方法,过滤掉空字符串后再展示,同时重新编号:

def display_notes(self):
    print('Notes\n-----')
    # 过滤掉空字符串和仅含空格的无效笔记
    valid_notes = [note for note in self.notes if note.strip()]
    for index, note in enumerate(valid_notes, 1):
        print(f'Note {index}: {note}')

这样展示时只会显示有效的非空笔记,序号也会重新从1开始排列。

完整修改后的代码

class Note:
    def __init__(self):
        self.notes = []
    
    def create_note(self, new_note):
        if new_note.lower() != 'out':
            self.notes.append(new_note)
    
    def delete_note(self, note_index):
        if 0 <= note_index < len(self.notes):
            del self.notes[note_index]
        else:
            print('ERROR: Invalid note index.')

    def display_notes(self):
        print('Notes\n-----')
        for index, note in enumerate(self.notes, 1):
            print(f'Note {index}: {note}')

notes = Note()

create = input('[Create Notes?]: ').lower()

if create == 'yes':
    c_note = 1
    n_note = ''

    print('Type "out" if you want to stop making notes.')

    while n_note.lower() != 'out':
        n_note = input(f'Note {c_note}: ')
        notes.create_note(n_note)
        c_note += 1

    d_note = input('Delete any Notes?: ').lower()

    if d_note == 'yes':
        try:
            notes_d = int(input('How many notes to delete?: '))
            for _ in range(notes_d):
                notes.display_notes()
                w_note = int(input('Which note to delete?: '))
                if 1 <= w_note <= len(notes.notes):
                    notes.delete_note(w_note - 1)
                else:
                    print('ERROR: Invalid note index.')
        except ValueError:
            print('ERROR: Invalid input for note index.')

    notes.display_notes()

elif create == 'no':
    print('Ok.')

else:
    print('ERROR: Not an option.')

内容的提问来源于stack exchange,提问作者anon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 15:07:12