Python初学者(需要帮助)-程序只输出保存列表的一个元素(3.8.1) Python初学者需要帮助

Python初学者(需要帮助)-程序只输出保存列表的一个元素(3.8.1) Python初学者需要帮助,python,python-3.x,Python,Python 3.x,我的程序是一个正确或错误的测验,用户输入保存的数据,它分两部分工作 第1部分:程序询问用户有多少问题。然后,用户填写一系列关于该金额的问题和答案。如果为问题提供的答案不是(真)或(假),程序将重新要求答案。然后,用户保存以使用此数据,以便在第2部分中学习 def vf(): import pickle import random import os os.system('cls') v = input("How m

我的程序是一个正确或错误的测验,用户输入保存的数据,它分两部分工作

第1部分:程序询问用户有多少问题。然后,用户填写一系列关于该金额的问题和答案。如果为问题提供的答案不是(真)或(假),程序将重新要求答案。然后,用户保存以使用此数据,以便在第2部分中学习

def vf():
        import pickle
        import random
        import os
        os.system('cls')

        v = input("How many questions do you have : ")
        vvff = list()

        for i in range(0, int(v)):
            v = input("Enter question : ")
            while True:
                f = input("Enter answer to that question (true or false) : ")
                if f.lower() in ('true', 'false', 'True', 'False'):
                    continue
                else:
                    print("This answer is invalid, please enter True or False")
        vf = {"question": v, "answer": f}
        vvff.append(vf)

        question_vff = input("Would you like to save (yes or no) ")
        if (question_vff == 'yes'):
                pickle.dump(vvff, open("Save.dat", "wb"))
                print("Saved!")
        if (question_vff == 'no'):
                print ("Please save to use you're data.")
第2部分:用户使用之前保存的数据回答正确或错误的测验。此测验将问题和答案混合起来,用户将一一回答。如果用户答对了,程序会说干得好,如果用户答错了,程序会说错误的答案

def vf2():
        import pickle
        import random
        import os
        os.system('cls')

        vvff = pickle.load(open("Save.dat", "rb"))
        random.shuffle(vvff)

        for f in vvff:
                print(f["question"])
                response = input("What was the answer to that question? : ")
                if (response == f["answer"]):
                        print("Good answer!")
                else:
                        print("Wrong answer...")
                        print("The answer is", f, ".")
        print("The study/quiz session is now over. Either create new data or try again later.")

我的问题是,在第2部分中,程序应该从保存的数据中洗牌问题,并向用户询问每一个问题。然而,它只询问他们在课程第一部分中输入的最后一个问题。这是什么原因造成的?我该如何修复它?非常感谢您的帮助。

这里和那里有一些小错误,因此我将立即去纠正它们:

def vf():
进口泡菜
随机输入
导入操作系统
操作系统(“cls”)
n=输入(“你有多少个问题:)#将v改为n以避免重叠
vvff=列表()
对于范围(0,int(n))中的i:
v=输入(“输入问题:”)
尽管如此:
f=输入(“输入该问题的答案(对或错):”)
如果f.lower()在('true','false'):#中去掉了大写字母,那么它已经是小写的了
持续
其他:
打印(“此答案无效,请输入True或False”)
vf={“问题”:v,“答案”:f}#移动到循环内部
附加(vf)#移动到循环内部
问题\u vff=输入(“您想保存(是或否)”)
如果(问题_vff==‘是’):
pickle.dump(vvff,打开(“Save.dat”、“wb”))
打印(“已保存!”)
如果(问题_vff==‘否’):
打印(“请保存以使用您的数据。”)

这里和那里都有一些小错误,所以我只需一次纠正它们:

def vf():
进口泡菜
随机输入
导入操作系统
操作系统(“cls”)
n=输入(“你有多少个问题:)#将v改为n以避免重叠
vvff=列表()
对于范围(0,int(n))中的i:
v=输入(“输入问题:”)
尽管如此:
f=输入(“输入该问题的答案(对或错):”)
如果f.lower()在('true','false'):#中去掉了大写字母,那么它已经是小写的了
持续
其他:
打印(“此答案无效,请输入True或False”)
vf={“问题”:v,“答案”:f}#移动到循环内部
附加(vf)#移动到循环内部
问题\u vff=输入(“您想保存(是或否)”)
如果(问题_vff==‘是’):
pickle.dump(vvff,打开(“Save.dat”、“wb”))
打印(“已保存!”)
如果(问题_vff==‘否’):
打印(“请保存以使用您的数据。”)

您可以对python3.8使用
:=

import os
import pickle
import random
from pathlib import Path

FILE = "Save.dat"


def clear():
    os.system("cls")


def vf():
    clear()
    vvff = []
    for _ in range(int(input("How many questions do you have: "))):
        q = input("\nEnter question: ")
        prompt = "Enter answer to that question (true or false): "
        while (a := input(prompt)).lower() not in ("true", "false"):
            print("This answer is invalid, please enter True or False")
        vvff.append({"question": q, "answer": a})
    tip = "\nWould you like to save [(yes)/no] "
    while input(tip).strip().lower() not in ("yes", "y", ""):
        print("Please save to use your data.\n")
    Path(FILE).write_bytes(pickle.dumps(vvff))
    print("Saved!")


def vf2():
    clear()

    vvff = pickle.loads(Path(FILE).read_bytes())
    random.shuffle(vvff)

    for qa in vvff:
        print(qa["question"])
        response = input("What was the answer to that question? ")
        if (response == qa["answer"]):
            print("Good answer!\n")
        else:
            print("Wrong answer...")
            print(f"The answer is {qa} .\n")
    print("The study/quiz session is now over. Either create new data or try again later.")

def main():
    vf()
    vf2()


if __name__ == "__main__":
    main()

您可以对python3.8使用
:=

import os
import pickle
import random
from pathlib import Path

FILE = "Save.dat"


def clear():
    os.system("cls")


def vf():
    clear()
    vvff = []
    for _ in range(int(input("How many questions do you have: "))):
        q = input("\nEnter question: ")
        prompt = "Enter answer to that question (true or false): "
        while (a := input(prompt)).lower() not in ("true", "false"):
            print("This answer is invalid, please enter True or False")
        vvff.append({"question": q, "answer": a})
    tip = "\nWould you like to save [(yes)/no] "
    while input(tip).strip().lower() not in ("yes", "y", ""):
        print("Please save to use your data.\n")
    Path(FILE).write_bytes(pickle.dumps(vvff))
    print("Saved!")


def vf2():
    clear()

    vvff = pickle.loads(Path(FILE).read_bytes())
    random.shuffle(vvff)

    for qa in vvff:
        print(qa["question"])
        response = input("What was the answer to that question? ")
        if (response == qa["answer"]):
            print("Good answer!\n")
        else:
            print("Wrong answer...")
            print(f"The answer is {qa} .\n")
    print("The study/quiz session is now over. Either create new data or try again later.")

def main():
    vf()
    vf2()


if __name__ == "__main__":
    main()

只是错误的标签。。。重新对齐
vf={“问题”:v,“回答”:f}
vvff.append(vf)
for loop
内,只是错误的选项卡。。。重新对齐
vf={“问题”:v,“回答”:f}
vvff.append(vf)
for循环内