Python 购物清单文本应用,保存问题
Python shopping list text application, saving issues
我正在努力学习 python 作为一个项目,我开始制作购物清单文本脚本。该脚本应该询问您是否要 add/delete 一个项目到您的列表中。它还具有打印列表的功能。您可以将列表保存为 .txt 文档,并在需要时继续。
我的第一个问题是,当我保存列表项并将它们取回时,所有不同的列表项都变成了一个列表项。所以我可以添加,但无法从列表中删除单个项目。
我现在尝试从 .txt 文档中拆分列表。我认为这会拆分列表,但现在每次我启动脚本时它都会添加额外的符号保存它,然后再次启动它。我可以做一些小的调整还是我的想法离我很远?
#I think the main problem is in the program_start_list_update_from_file defenition
# Shopping list simulator
shoppingList = []
def program_start_list_update_from_file():
global shoppingList
outputname = "shoppinglistfile.txt"
myfile = open(outputname, 'r')
lines = str(myfile.read().split(', '))
shoppingList = [lines]
myfile.close()
def shopping_list_sim():
print("Would you like to add (a) delete (d) or list (l) items in your shopping list?")
print('Press (e) for exit and (s) for list saving')
playerInput = input()
outputname = "shoppinglistfile.txt"
try:
if playerInput == "a":
print("What item would you like to add?")
shoppingList.append(input())
print("Item added")
shopping_list_sim()
elif playerInput == "d":
print("What item would you like to remove?")
print(shoppingList)
shoppingList.remove(input())
print("Item removed")
shopping_list_sim()
elif playerInput == "l":
myfile = open(outputname, 'r')
yourResult = ', '.join(myfile)
print(yourResult)
shopping_list_sim()
elif playerInput == "e":
print("Exiting program")
sys.exit()
elif playerInput == "s":
myfile = open(outputname, 'w')
myfile.write(', '.join(shoppingList))
myfile.close()
shopping_list_sim()
else:
print("Please use the valid key")
shopping_list_sim()
except ValueError:
print("Please put in a valid list item")
shopping_list_sim()
program_start_list_update_from_file()
shopping_list_sim()
问题的根源是
lines = str(myfile.read().split(', '))
shoppingList = [lines]
您正在将文件拆分为一个列表,从该列表中创建一个字符串,然后将该字符串添加到列表中。
shoppingList = myfile.read().split(', ')
足以满足您的需求:split
为您创建一个列表。
您应该考虑从递归调用切换到循环:
每个递归调用都会增加开销,因为它会构建一个新的 stack frame,在这种情况下,这是完全没有必要的。
按照您当前的方式,每个新提示都有一个新的堆栈框架:
shopping_list_sim()
shopping_list_sim()
shopping_list_sim()
shopping_list_sim()
...
如果在循环中执行,则不会递归构建堆栈。
我正在努力学习 python 作为一个项目,我开始制作购物清单文本脚本。该脚本应该询问您是否要 add/delete 一个项目到您的列表中。它还具有打印列表的功能。您可以将列表保存为 .txt 文档,并在需要时继续。
我的第一个问题是,当我保存列表项并将它们取回时,所有不同的列表项都变成了一个列表项。所以我可以添加,但无法从列表中删除单个项目。
我现在尝试从 .txt 文档中拆分列表。我认为这会拆分列表,但现在每次我启动脚本时它都会添加额外的符号保存它,然后再次启动它。我可以做一些小的调整还是我的想法离我很远?
#I think the main problem is in the program_start_list_update_from_file defenition
# Shopping list simulator
shoppingList = []
def program_start_list_update_from_file():
global shoppingList
outputname = "shoppinglistfile.txt"
myfile = open(outputname, 'r')
lines = str(myfile.read().split(', '))
shoppingList = [lines]
myfile.close()
def shopping_list_sim():
print("Would you like to add (a) delete (d) or list (l) items in your shopping list?")
print('Press (e) for exit and (s) for list saving')
playerInput = input()
outputname = "shoppinglistfile.txt"
try:
if playerInput == "a":
print("What item would you like to add?")
shoppingList.append(input())
print("Item added")
shopping_list_sim()
elif playerInput == "d":
print("What item would you like to remove?")
print(shoppingList)
shoppingList.remove(input())
print("Item removed")
shopping_list_sim()
elif playerInput == "l":
myfile = open(outputname, 'r')
yourResult = ', '.join(myfile)
print(yourResult)
shopping_list_sim()
elif playerInput == "e":
print("Exiting program")
sys.exit()
elif playerInput == "s":
myfile = open(outputname, 'w')
myfile.write(', '.join(shoppingList))
myfile.close()
shopping_list_sim()
else:
print("Please use the valid key")
shopping_list_sim()
except ValueError:
print("Please put in a valid list item")
shopping_list_sim()
program_start_list_update_from_file()
shopping_list_sim()
问题的根源是
lines = str(myfile.read().split(', '))
shoppingList = [lines]
您正在将文件拆分为一个列表,从该列表中创建一个字符串,然后将该字符串添加到列表中。
shoppingList = myfile.read().split(', ')
足以满足您的需求:split
为您创建一个列表。
您应该考虑从递归调用切换到循环: 每个递归调用都会增加开销,因为它会构建一个新的 stack frame,在这种情况下,这是完全没有必要的。
按照您当前的方式,每个新提示都有一个新的堆栈框架:
shopping_list_sim()
shopping_list_sim()
shopping_list_sim()
shopping_list_sim()
...
如果在循环中执行,则不会递归构建堆栈。