【发布时间】:2021-03-14 20:08:39
【问题描述】:
我正在尝试制作一个闪存卡生成器,但我无法从文本文件中收集输入。当程序询问用户问题和答案时,它会将信息放入一个文本文件中,以便稍后在用户希望查看信息时使用。但是,每当程序第一次运行时,前两个输入都会在文本文件中重复两次。
这个错误的一个例子是这样的:
What is the capitol of New York, RochesterWhat is the Capitol of New York, Rochester .
这是我为完成任务而编写的代码:
user_inputs = []
f = open('flash_card.txt', 'a')
print('Hello! Welcome to Flash Card Study Helper:)')
create_add = input('Do you wish to create new cards? ')
while create_add == ('yes'):
question = input('Please type out the question: ')
answer = input('Please type out the answer: ')
combined = ','.join(user_inputs)
f.write(combined+'\n')
user_inputs.append(combined)
create_add =input('Do you wish to create another card? ')
else:
print(user_inputs)
为什么我的输入在写入文件时会重复?
【问题讨论】:
-
您的代码按预期工作,没有任何重复数据。如果您仍然遇到与使用
with()命令打开文件相同的问题。
标签: python python-3.x list file