简体   繁体   English

为什么我在运行程序时输入的前两个输入会在文本文件中重复?

[英]Why do the first two inputs I enter when I run my program repeat in the text file?

I'm trying to make a flash card generator, and I'm having trouble gathering the inputs from the text file.我正在尝试制作 flash 卡生成器,但我无法从文本文件中收集输入。 When the program asks the user for the questions and answers, it drops the information into a text file that will be used later, when the user wishes to review the information.当程序询问用户问题和答案时,它会将信息放入一个文本文件中,以便稍后在用户希望查看信息时使用。 However, whenever the program runs for the first time, the first two inputs are repeated twice in the text file.但是,每当程序第一次运行时,前两个输入在文本文件中重复两次。

An example of this bug is like so:这个错误的一个例子是这样的:

What is the capitol of New York, RochesterWhat is the Capitol of New York, Rochester .

Here is the code I've written to achieve my task:这是我为完成任务而编写的代码:

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)

Why do my inputs duplicate when written into the file?为什么我的输入在写入文件时会重复?

You are tracking ALL of the user's inputs in user_input , and then writing that to your file every time through the loop.您正在跟踪user_input中的所有用户输入,然后每次通过循环将其写入您的文件。 So, first time, you write q1, a1.所以,第一次,你写 q1, a1。 Next time, you write q1, a1, q2, a2.下一次,你写 q1, a1, q2, a2。 Next time, you write q1, a1, q2, a2, q3, a3.下一次,你写 q1, a1, q2, a2, q3, a3。 If you really want to update the file during every loop, you should only write the new stuff:如果你真的想在每个循环中更新文件,你应该只写新的东西:

    q_a = question + ',' + answer
    f.write(q_a+'\n')
    user_inputs.append(q_a)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我运行我的程序时,我的输入没有显示出来。 为什么? - My inputs do not show up when I run my program. Why? 当我猜第一个答案错误但随后输入正确答案时,我的代码不会重复 - my code does not repeat when I guess the first answer wrong but then enter the correct answer 我有两个(.py)文件。 当第一个程序结束时,它将自行关闭,然后打开并运行第二个程序文件。 我该怎么做? - I have got two (.py) files. When the first program comes to end, it will close itself then open and run the second program file. How can ı do it? 为什么我在运行此程序时会收到 Terminator 错误? - Why do I get a Terminator error when I run this program? 为什么我的python程序双击.sh文件不运行 - Why does my python program not run when I double click the .sh file 当我在程序中输入单词时,如何使程序接受任何情况 - How do I get my program to accept any case when I enter a word into it 当我运行我的程序时,为什么没有发生任何事情 - why is nothing happening when i run my program 为什么我的程序在运行时没有响应 - Why does my program not respond when I run it 为什么我的分支条件不执行? 而且,我如何让程序重复自己? - Why won't my branched conditions execute? And, how do I get the program to repeat itself? 运行此代码并输入信息时,为什么会出现“名称错误:名称”…“未定义”? - Why do I get a “name error: name ”…“ is not defined ” when I run this code and enter information?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM