0

I'm trying to make a quiz maker and loader in python 3.9 and I'm wondering how do I save a list of questions and answers. This is so you can make a list then save it to a file and load it to answer it.

    questions = [
        input("Enter question 1: "),
        input("Enter question 2: "),
        input("Enter question 3: "),
        input("Enter question 4: "),
        input("Enter question 5: "),
        input("Enter question 6: "),
        input("Enter question 7: "),
        input("Enter question 8: "),
        input("Enter question 9: "),
        input("Enter question 10: "),
    ]

2 Answers2

2

Given your usecase, I don't think it would be wise to use a pickle file. Instead I would suggest to convert the list into a string and store it in a simple txt file. Here is a sample of how you can do it:

questions = [
        input("Enter question 1: "),
        input("Enter question 2: "),
        input("Enter question 3: "),
        input("Enter question 4: "),
        input("Enter question 5: "),
        input("Enter question 6: "),
        input("Enter question 7: "),
        input("Enter question 8: "),
        input("Enter question 9: "),
        input("Enter question 10: "),
    ]

with open('outfile.txt','w') as f:
    f.write('SEP'.join(questions)) #You can replace the SEP with a newline or say a pipe operator (|) or something similar
    
with open('outfile.txt') as f:
    print(f.read().split("SEP")) #Returns the list of questions as you have earlier
paradocslover
  • 2,932
  • 3
  • 18
  • 44
1

python's pickle library is often a good choice, but it is a file format that is unreadable by humans. If you want the convenience of being able to open up a list of questions in a simple text editor, I'd recommend simply saving your list of questions as lines in a txt file. You've already figured out how to get a list of user inputs, so you just need to iterate over that list of strings, and write them to a file.

#create a new txt file and open it in write mode
with open(r"C:\some\file\path.txt", "w") as f: #use a 'raw' string to handle windows file paths more easily
    for question in questions:
        f.write(question + "\n") #add a newline to the end of the question so each question is on it's own line in the text file

Reading the file back into python is similarly easy, but you could take the time to add a few features like ignoring blank lines, and perhaps even including a syntax for comments which should be ignored.

#open the file in read mode
questions = [] #create an empty list to read the questions into from the file
with open(r"C:\some\file\path.txt", "r") as f:
    for line in f:
        if line.strip(): #if we strip whitespace from the front and back of a line is there any text left
            if not line.strip().startswith("#"): #if the first non whitespace character is a '#', treat the line as a comment and skip it
                questions.append(line)
Aaron
  • 10,133
  • 1
  • 24
  • 40
  • What if it is a multiline question? Why do you need to specify the 'r' read mode since it is default. This answer has a lot of pitfalls and redundancies!! Also, the comment part (#)... where did that come from? – paradocslover May 10 '21 at 03:38
  • @paradocslover I specify `"r"` mode for clarity for the sake of a new programmer. It can be omitted. Multi-line entry is not realistic if the input is coming from `input()`, and using newline as the separator makes it more human readable. If the intent is to include human readability to the file type, and there is an expectation that a use might use a text editor, it is very likely to end up with extra blank lines. The comment syntax was just a quick thought I had to add some value to taking the time to read my answer... – Aaron May 10 '21 at 03:59