提问人:vasi 提问时间:6/22/2023 更新时间:6/23/2023 访问量:38
使用字符串的枚举函数,并插入文件提取变量,是否可以在输入时调用相关的枚举数字?
Using an enumerate function for a string, with inserted file extracted variables, is it possible to recall correlated enumerate numbers upon input?
问:
我对 Python 很陌生,所以不确定任何事情:) 通过定义一个函数,我能够使用从文本文件中提取的信息对任务进行编号。
def view_mine():
with open("tasks.txt", "r") as file:
for i, lines in enumerate(file):
temp = lines.strip()
temp = temp.split(", ")
if user_name in (temp[0]):
print("\n" + str(i+1) + ". " + f"Assigned to:\t{temp[0]}\nThe tital of the task:\t{temp[1]}\
\nThe description of the task :\t{temp[2]}\nSet date:\t{temp[3]}\
\nDue date:\t{temp[4]}\nTask complete? :\t{temp[5]}")
file.close()
然后,我需要使用该数字,以便要求用户输入他/她希望编辑的任务数量,也就是更改截止日期/完成状态。
view_mine()
print("\n")
task_selection = input("Select a task for modification? (enter -1 to return to the main menu): ")
task_selection = int(task_selection)
task_selection = task_selection - 1
task_selection = view_mine(task_selection) ???- here is where I am stuck
if task_selection != ("-1"):
task_modification = input("Would you like to:\n1-edit the task\n2-mark the task as complete\nEneter your choice: ")
if task_modification == ("1"):
task_modification_edit = input("Would you like to:\n1-edit the username the task is assigned to\n2-edit the due date\nEnter your choice: ")
my_task = {f"Assigned to: " : (temp[0]),
"\nThe tital of the task: " : (temp[1]),
"\nThe description of the task: " : (temp[2]),
"\nSet date: " : (temp[3]),
"\nDue date: " : (temp[4]),
"\nTask complete? " : (temp[5])}
if task_modification_edit == ("1"):
task_modification_username = input("Enter the username you wish to assign the task for: ")
my_task["Assigned to: "] = task_modification_username
print("The uasername has been sucsessfuly changed")
elif task_modification_edit == ("2"):
task_modification_due_date = input("Please enter a new due date: ")
my_task["\nDue date: "] = task_modification_due_date
print ("The due date has been sucsesfully modified")
elif task_modification == ("2"):
if my_task["\nTask complete? "] == ("No"):
my_task["\nTask complete? "] = ("Yes")
print("The task has been marked as complite")
elif task_selection == ("-1"):
break
我不确定如何执行此操作,以获得允许用户首先选择任务编号,然后对所选任务进行修改的预期结果。
答:
0赞
JackColo_Ben4
6/23/2023
#1
那好吧。。。
我会给你一个愚蠢的解决方案,你可以把它作为构建程序并开始练习 Python 的起点!
您应该根据您真正想要实现的目标更改功能、修复缺陷或添加必要的改进。
提示:
_How重新启动程序而不是在出现错误时退出?
_How确保输入是有效的整数?
_How处理其他异常情况?(例如,当数字对应于不存在的任务时发生的“IndexError:列出超出范围的索引”)
_How检查日期格式是否正确?
_How使用“-1”作为命令返回每个输入的主菜单?
_How创建一个在更改后修改/更新原始文件的方法?
_How从输入添加新任务?
_How创建一个简单的 GUI 界面,而不是使用终端?
片段:
def view_mine(user_name):
tasks = []
with open("text_stack_prova.txt", "r") as file:
for lines in file:
temp = lines.strip().split(", ")
if user_name == temp[0]:
tasks.append(temp)
file.close()
return tasks
if __name__=="__main__":
while True:
user_name = input("Enter the user you are looking for:\n")
tasks = view_mine(user_name)
if len(tasks) == 0:
print(f"No tasks found for user: {user_name}! Exiting program...")
break
else:
print("Here are the Tasks assigned to", user_name + ":")
for i, task in enumerate(tasks):
print(str(i+1) + ". " + f"Assigned to:\t{task[0]}\nThe title of the task:\t{task[1]}\
\nThe description of the task:\t{task[2]}\nSet date:\t{task[3]}\
\nDue date:\t{task[4]}\nTask complete? :\t{task[5]}\n")
task_selection = input("Give me the index of the Task that need to be modified: (enter -1 to return to the main menu): ")
try:
task_selection = int(task_selection)
if task_selection == -1:
print("Ok let's restart from the beginning...\n")
continue # not break!!
else:
task_selection -= 1
selected_task = tasks[task_selection]
task_modification = input("Would you like to:\n1 - edit the task\n2 - mark the task as complete\nEnter your choice: ")
while True:
if task_modification == "1":
task_modification_edit = input("Would you like to:\n1 - edit the username the task is assigned to\n2 - edit the due date\nEnter your choice: ")
if task_modification_edit == "1":
task_modification_username = input("Enter the username you wish to assign the task to: ")
old_name = selected_task[0]
selected_task[0] = task_modification_username
print("The user {} has been successfully changed in {}".format(old_name, task_modification_username))
break
elif task_modification_edit == "2":
task_modification_due_date = input("Please enter a new due date [YYYY-MM-DD]: ")
selected_task[4] = task_modification_due_date
print("The due date has been successfully modified.")
break
else:
print("Invalid choice. Please try again.")
continue
elif task_modification == "2":
if selected_task[5] == "No":
selected_task[5] = "Yes"
print("The task has been marked as complete.")
else:
print("The task is already marked as complete.")
break
else:
print("Invalid choice. Please try again. Exiting program...")
except ValueError:
print("Invalid input. Please enter a valid number or -1.")
break
评论
0赞
vasi
6/27/2023
非常感谢您的回答!!
0赞
JackColo_Ben4
6/28/2023
想象!感谢您的反馈!
评论