gpt4 book ai didi

python - 如何在Python中检查数组的所有元素是否已填充

转载 作者:行者123 更新时间:2023-12-01 08:13:10 24 4
gpt4 key购买 nike

我正在使用数组或列表在 Python 中制作一个待办事项列表应用程序。我想检查包含所有“待办任务”的数组是否已满。如果已满,我会通知用户列表已满。 我还是个初学者。

todo_list = ["1.)", "2.)", "3.)", "4.)", "5.)", "6.)", "7.)", "8.)", "9.)", "10.)"]
def addTask(taskName):

'''
this is a global variable to keep track of what index the last task was
placed in.
'''


global x
x = int(x)
num = x + 1
num = int(num)
taskName = str(taskName)

'''
This is what I tried to make the program start from the beginning if the
list was full.
'''

if x > list_length:
x = 0
todo_list[0] = None


todo_list[x] = str(num) + ".) " + taskName
x = x+1

print("Done!")
main()

最佳答案

您是说您已将可能的任务数量限制为 10 个吗?如果每个列表都附加了一项任务,那么列表应该通知用户它已满?

如果是这样,您就知道空任务是“10.)”,因此它的最大长度为 4(4 个字符),因此如果任何项目长度小于或等于 4,则它是空的

for task in todo_list:
if len(task) > 4:
print('Todo list is full')
break
else:
print('Todo list is full')

我还可以建议更好的方法来创建待办事项列表吗?使用字典。

todo_list = {'clean cat':'incomplete', 'buy milk':'complete'}

添加新任务非常简单!

todo_list['learn python'] = 'incomplete'

并且更新任务更加容易!

todo_list['clean cat'] = 'complete'

这是我的做法:

todo_list = {}

if len(todo_list) == 10:
print('Sorry, list is full!')
else:
task_name = input('Task name: ')
todo_list[task_name] = 'incomplete'

print(todo_list)

关于python - 如何在Python中检查数组的所有元素是否已填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55112351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com