나는 python을 배우려고 노력 중이며 프로젝트로 쇼핑 목록 텍스트 스크립트를 만들기 시작했습니다. 이 스크립트는 목록에 항목을 추가/삭제할지 묻는 메시지를 표시합니다. 그것은 또한 귀하의 목록을 인쇄하는 기능이 있습니다. 목록을 .txt 문서로 저장하고 원하는 때에 계속할 수 있습니다.Python 쇼핑 목록 텍스트 응용 프로그램, 절약 문제
첫 번째 문제는 목록 항목을 저장하고 다시 가져 왔을 때 모든 다른 목록 항목이 하나의 목록 항목이되었다는 것입니다. 그래서 추가 할 수는 있지만 목록에서 단수 항목을 제거 할 수는 없습니다.
이제는 .txt 문서에서 목록을 분할하려고했습니다. 나는 이것이리스트를 분리한다고 생각하지만, 이제는 스크립트를 시작할 때마다 여분의 심볼을 추가하여 저장하고 다시 시작한다. 제가 할 수있는 작은 조정이 있습니까, 아니면 아이디어 마일이 떨어져 있습니까? 그런 다음 추가, 목록에 파일을 분할이 목록에서 문자열을 만들고있어
lines = str(myfile.read().split(', '))
shoppingList = [lines]
#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()
일부 예제 출력과 저장된 목록의 샘플을 제공 할 수 있습니까? – TemporalWolf
예, 키, 바나나 및 당근을 추가했습니다. 처음 저장 한 .txt 파일은 [ ''], 키, 바나나, 당근. 두 번째 시간은 프로그램을 열고 마우스를 추가 한 후 저장하는 것입니다. 결과 : [ "[ '']", '키', '바나나', '당근'], 마우스. 세 번째로 단어 인스턴스를 추가하고 저장합니다. 결과 : [ '['[\ '\'] '', '키' ',' '바나나' ',' '당근'] '', '마우스'] 인스턴스. @TemporalWolf –