我的问题是我的列表(lst)没有存储在文件中。你拥有的美好社区。我是该网站的新手,所以请在评论中注意这一点。 以下是我很久以前能够存储它的代码,但有问题,所以我取消了它,出于某种原因,在我的脑海中,我不记得我做了什么来存储它!
days = ["sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
answered = False
while answered == False:
userInput = input("What day of the week would you like to assign a event: ")
lst = []
input_words=userInput.lower().split()
for word in input_words:
if word in days:
lst.append(word)
print(word)
# set answered to True boolean
answered = True
print((lst))
# only evaulated after reviewing all words, if True is not set, prompts user again and let's them know that their answer is not valid
if answered == False:
print("You typed days of week wrong! Try Again!")
def listToString():
if 0 < 2:
test = open('hold.txt', 'a')
print(test.write(lst + '\n'))
test.close()
return listToString
'a+' 不起作用!!在复制 我已经尝试了很多事情,但是我真的很相信我,我有一个解释。但是当我将 lst 放入文件时,我希望它在每一行上间隔开,所以对于每个项目,我希望它在一个新行上,但不仅仅是存储 1 个项目。 我是这个网站的新手。 也有人想告诉我为什么我的代码不断给我输出似乎是我写入文件的列表中的 1 个项目的大小
答案 0 :(得分:2)
以下几点需要修改:
listToString()
方法days = ["sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
answered = False
while answered == False:
userInput = input("What day of the week would you like to assign a event: ")
lst = []
input_words=userInput.lower().split()
for word in input_words:
if word in days:
lst.append(word)
print(word)
# set answered to True boolean
answered = True
print((lst))
# only evaulated after reviewing all words, if True is not set, prompts user again and let's them know that their answer is not valid
if answered == False:
print("You typed days of week wrong! Try Again!")
def listToString():
if 0 < 2:
test = open('hold.txt', 'a')
# and list is not able to con-cat with str, so need to convert that too
test.write(str(lst) + '\n')
test.close()
return listToString
# you need to call the method to execute the method code
listToString()
答案 1 :(得分:1)
您是要调用您的函数吗:
def listToString(lst):
with open('hold.txt', 'a') as test:
test.write(str(lst) + '\n')
listToString(lst)
答案 2 :(得分:1)
days = ["sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
answered = False
while answered == False:
userInput = input("What day of the week would you like to assign a event: ")
lst = []
input_words=userInput.lower().split()
for word in input_words:
if word in days:
lst.append(word)
print(word)
# set answered to True boolean
answered = True
print(lst)
# only evaluated after reviewing all words, if True is not set, prompts user again and let's them know that their answer is not valid
if answered == False:
print("You typed days of week wrong! Try Again!")
def listToString():
with open('hold.txt', 'w') as f:
for day in lst:
f.write(day + '\n')
listToString()
还建议使用 with
打开和关闭您的文件。您可以找到有关它的更多信息 here. 我将每天的条目打印在单独的一行中,如果您只想将列表打印在一行上,您可以将其改回并添加 str(lst)
。
答案 3 :(得分:1)
我检查了你的代码。为了让您知道 quamrana 所说的是必要的,我运行了代码并认为您可能会在两件事上失败:
def listToString():
if 0 < 2:
test = open('hold.txt', 'a')
test.writelines(line+'\n' for line in lst)
test.close()
return
if __name__=="__main__":
days = ["sunday","monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
answered = False
while answered == False:
userInput = input("What day of the week would you like to assign a event: ")
lst = []
input_words=userInput.lower().split()
for word in input_words:
if word in days:
lst.append(word)
print(word)
# set answered to True boolean
answered = True
print((lst))
# only evaulated after reviewing all words, if True is not set, prompts user again and let's them know that their answer is not valid
if answered == False:
print("You typed days of week wrong! Try Again!")