首页 文章

为什么这个Python字典会反复写入文件?

提问于
浏览
0

我的程序具有将问题添加到字典中的功能,“问题”稍后被写入文本文件 . 我添加了输入另一个问题的选项,而不是再次浏览菜单;你能解释一下为什么这段代码:

def add_info():

    #ask what IF want to add fact
    decision = input("You've selected '1' to add a fact. Are you sure you would you like to add a fact? \
    \n(Respond with 'y' or 'n')\n").title()
    #IF they WANT TO ADD A FACT
    if decision == 'Y':
        print('I am going to ask for the question and the answer next.')

    while True:
        review2 = open('review.txt', 'r')
        reviewing = review2.readlines()
        review2.close()
        x=len(reviewing)+1
        question = input('Please type the question you want displayed:\n')
        questionz.append(question)
        answer = str(input('Please type the answer you want displayed\n'))
        solution = {
        }
        solution[question] = answer
        questions[x]= solution
        review = open('review.txt', 'a')
        review.write(str(questions))
        review.write("\n")
        review.close()
        #asks if want to add ANOTHER
        decision2= input('Would you like to add another fact?\
        \n(Please answer "y" ot "n")\n').title()
        if decision2 == "N":
            menu()
            break
        elif decision2 =='Y':
            continue

当我尝试在函数中添加另一个问题时写这个吗?:

{1: {'3+3?': '6'}}
{2: {'4+5?': '9'}}
{3: {'2+6?': '8'}}
{4: {'5?': '5'}}
{4: {'5?': '5'}, 5: {'6?': '6'}}

(问题1,2和3已经单独添加,我输入'5?'作为问题,'5'作为答案,'6?'作为下一个问题,'6'作为下一个答案,并重写第五个问题代替第五个问题,然后添加一个逗号 . 我没有重新输入任何内容 . )

1 回答

  • 0

    我相信这就是你所追求的:

    import os
    
    
    def add_info():
        # ask what IF want to add fact
        decision = input("You've selected '1' to add a fact. Are you sure you would you like to add a fact? \
        \n(Respond with 'y' or 'n')\n").upper()
    
        # IF they WANT TO ADD A FACT
        if decision == 'Y':
            print('I am going to ask for the question and the answer next.')
            questions = []
    
            if os.path.isfile('review.txt'):
                review = open('review.txt', 'r', encoding='utf-8')
                index = len(review.readlines()) + 1
                review.close()
            else:
                index = 1
    
            while True:
                question = input('Please type the question you want displayed:\n')
    
                answer = input('Please type the answer you want displayed:\n')
    
                nextQuestion = {}
                nextQuestion[index] = {}
                nextQuestion[index][question] = answer
    
                questions.append(nextQuestion)
    
                index += 1
    
                if input('Would you like to add another fact?\n(Please answer "y" ot "n")\n').upper() == 'N':
                    break;
    
            if os.path.isfile('review.txt'):
                review = open('review.txt', 'a', encoding='utf-8')
                for x in questions:
                    review.write(str(x) + '\n')
                review.close()
            else:
                review = open('review.txt', 'w')
                for x in questions:
                    review.write(str(x) + '\n')
                review.close()
    
    
    add_info()
    

    我使用os.path测试文件's existence so you wouldn' t获取错误,如果它不存在 . 我还移动了一些东西,所以代码更容易阅读,你的代码中有一些错误,如 questionzquestions 可能试图引用相同的变量 . 希望这可以帮助 .

相关问题