2 回答
TA贡献1824条经验 获得超8个赞
您可能只是有一些额外的缩进。这是相同的脚本,但在第 19 和 20 行删除了一些缩进。
#!/usr/bin/env python3
import os
import smtplib
import csv
os.system('clear')
class CreateList(object):
def add_items(self):
shop_list = []
print("Lets create a shopping list for you..\n")
print("Please enter DONE when you have all the items needed for your shopping list.")
while True:
add_item = input("> ")
if add_item == 'DONE':
break
shop_list.append(add_item)
print("Here is your current shopping list:")
csv = open('shoplist.csv', 'w')
for item in shop_list:
print(item)
csv.write(item + '\n')
csv.close()
c = CreateList()
c.add_items()
TA贡献1786条经验 获得超13个赞
有一个else失踪。您input没有附加任何内容shop_list,因此没有任何内容写入文件。
import os
import smtplib
import csv
os.system('clear')
class CreateList(object):
def add_items(self):
shop_list = []
print("Lets create a shopping list for you..\n")
print("Please enter DONE when you have all the items needed for your shopping list.")
while True:
add_item = input("> ")
if add_item == 'DONE':
break
else: # <<< missing
shop_list.append(add_item)
print("Here is your current shopping list:")
csv = open('shoplist.csv', 'w')
for item in shop_list:
print(item)
csv.write(item + '\n')
csv.close()
c = CreateList()
c.add_items()
添加回答
举报
