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()
添加回答
舉報