由于某些原因,一旦循环到达待办事项列表中的第10项,它将开始打印每个待办事项列表项2x,如以下示例所示:
**Sample output:
1.Walk the dog
2.Make the bed
...
10.Take out the trash
11.Take out the trash
12.Call Sara
13.Call Sara**
...python
for item in mylist:
print(item)
name = input("What is your name?")
print(name + "\'s To-Do-List")
mylist = []
count = 1
while True:
newitem = input("What do you want to add to your to-do-list?")
if newitem == "nothing" or newitem == "NOTHING" or newitem == "Nothing":
print("\n")
print("Your to-do-list is complete!")
print("You have " + str(count - 1) + " items on your to-do-list: \n")
for item in mylist:
print(item)
break
print(str(count) + ": " + newitem)
for i in str(count):
mylist.append(newitem)
count += 1..。
发布于 2018-07-16 07:55:43
谢谢@Rakesh帮助清理这个问题,但我认为它仍然与问题中的观察结果不一致。在for循环之前的打印行可能也是缩进的。
我认为造成你的双重印刷的原因是:
for i in str(count):您将count变量抛出为字符串,因此从10开始的数字将迭代两次,1,0次,从100开始,3次,1,0,0次。本质上,这些是字符串中的字符数。
无论如何,在我看来,这里不需要for循环,除非您尝试做一些我还没有理解的事情。简单地说
print(str(count) + ": " + newitem)
mylist.append(newitem)
count += 1https://stackoverflow.com/questions/51356649
复制相似问题