我正在为大学二年级做准备,在我们被要求做的一项练习中,我有几个与 if 语句相关的问题
我们必须编写一个 Todo 应用程序,有一次练习要求我们用 elif 替换“显示”、“编辑”、“退出”和“完成”的 if 语句
问题是当我尝试用 elif 替换原来的 if 语句时,我的程序根本不起作用
elif语句执行前的代码
prompt = " Add or show or type exit to quit the programm or edit to edit a item in your list : " list = [] while True: useraction = input(prompt + " you can also right completed to remove an task that you have done ") useraction = useraction.strip() if "add" in useraction: todo = useraction[4:] with open("app.txt", "r") as file: list = file.readlines() list.append(todo) with open("app.txt", "w") as file: file.writelines(list) elif "completed" in useraction: num = int(input("Please right the number of the task that is completed")) with open("app.txt", "r") as file: list = file.readlines() list.pop(num - 1) print("That task has been completed, congratulation!") if "show" in useraction: with open("app.txt", "r") as file: list = file.readlines() for index, item in enumerate(list): item = item.strip("\n") row = f"{index + 1}-{item}" row = row.title() print(row) if "exit" in useraction: break if "edit" in useraction: number = int(input("please right the number of the item that you want to modify : ")) number = number - 1 with open("app.txt", "r") as file: list = file.readlines() new_todo = input("please right the new thing that you want to do : ") list[number] = new_todo with open("app.txt", "w") as file: file.writelines(list) print("Bye !") When i decide to put the elif statement in the code this is what happen : prompt = " Add or show or type exit to quit the programm or edit to edit a item in your list : " list = [] while True: useraction = input(prompt + " you can also right completed to remove an task that you have done ") useraction = useraction.strip() if "add" in useraction: todo = useraction[4:] with open("app.txt", "r") as file: list = file.readlines() list.append(todo) with open("app.txt", "w") as file: file.writelines(list) elif "completed" in useraction: num = int(input("Please right the number of the task that is completed")) with open("app.txt", "r") as file: list = file.readlines() list.pop(num - 1) print("That task has been completed, congratulation!") elif "show" in useraction: with open("app.txt", "r") as file: list = file.readlines() for index, item in enumerate(list): item = item.strip("\n") row = f"{index + 1}-{item}" row = row.title() print(row) elif "exit" in useraction: break elif "edit" in useraction: number = int(input("please right the number of the item that you want to modify : ")) number = number - 1 with open("app.txt", "r") as file: list = file.readlines() new_todo = input("please right the new thing that you want to do : ") list[number] = new_todo with open("app.txt", "w") as file: file.writelines(list) print("Bye !")
我有超过 16 个错误,其中大部分与以下相关:
预期声明,发现 Py:ELIF_KEYWORD 变量注释的非法目标预期表达式等…
从你提供的代码中,我可以看到你的 elif 语句的缩进不正确,导致出现语法错误。在 Python 中,缩进非常重要,它用于指示代码块的开始和结束。请注意 elif 语句应该与前面的 if 语句对齐,并且应该在同一级别的缩进下。
以下是修改后的代码示例,修复了缩进问题:
prompt = "Add or show or type 'exit' to quit the program or 'edit' to edit an item in your list: " lst = [] while True: useraction = input(prompt + "You can also type 'completed' to remove a task that you have done: ") useraction = useraction.strip() if "add" in useraction: todo = useraction[4:] with open("app.txt", "r") as file: lst = file.readlines() lst.append(todo) with open("app.txt", "w") as file: file.writelines(lst) elif "completed" in useraction: num = int(input("Please write the number of the task that is completed: ")) with open("app.txt", "r") as file: lst = file.readlines() lst.pop(num - 1) print("That task has been completed, congratulations!") elif "show" in useraction: with open("app.txt", "r") as file: lst = file.readlines() for index, item in enumerate(lst): item = item.strip("\n") row = f"{index + 1}-{item}" row = row.title() print(row) elif "exit" in useraction: break elif "edit" in useraction: number = int(input("Please write the number of the item that you want to modify: ")) number = number - 1 with open("app.txt", "r") as file: lst = file.readlines() new_todo = input("Please write the new thing that you want to do: ") lst[number] = new_todo with open("app.txt", "w") as file: file.writelines(lst) print("Bye!")
请注意,我还修改了变量名将 list 改为 lst,因为 list 是Python的内置类型名称,最好不要用作变量名,以避免混淆。
list
lst
另外,我还将一些提示信息进行了微调,确保它们更加易读和易懂。希望这可以解决你的问题!