0

I am making a program that asks a user a lot of questions, and I have each question defined at the top of my file. Unfortunately because of the ridiculous number of questions I need to have, the file has become extremely packed and difficult to navigate. The questions are organized by different sections, so I thought it would be great if I could fold all of the variables by section and label them with a comment.

I am using Pydev for Eclipse. I have done some searching but haven't found anything promising. Any suggestions for how to do this or how to better organize my variables?

2
  • 3
    Have you considered restructuring your questions into dicts? If so, you could store them in separate text files and access them with docs.python.org/2/library/pickle.html Commented Apr 1, 2014 at 1:59
  • Use dictionary , that will solve your problems. Commented Apr 1, 2014 at 2:01

2 Answers 2

1

"Data driven programming": store your questions in a data file, and your program just needs the required logic to load and present them.

Sign up to request clarification or add additional context in comments.

1 Comment

I did some research on data driven programming; it seems like it has everything to do with how your program handles data but not much to do with how it is organized within your code. Please correct me if thats wrong. Your suggestion about a data file is certainly helpful though, I will try out that approach.
0

The short answer is that you shouldn't have a lot of variables to manage, rather they should be organized in some way, using a list or a dictionary or some other technique.

The answer really depends on the nature of your questions and answers, but, for example, if I wanted to sum the answers together or plot the answers on a graph, it might make sense to have a list of questions and a corresponding list of answers:

qlist = [ "What's your chem grade", "What's your Math grade", . . . ]
alist = []
for q in qlist:
    a = <answer>
    alist.append(a)

Of course, if your questions and answers are not amenable to that approach, you could organize them some other way, for example, using a dictionary. One simple approach would be to use the question as the key, though that could get cumbersome.

5 Comments

Unfortunately my questions are not amendable to that approach, The questions are shown on a GUI so they are stored in an object which holds all of the questions to be displayed at the same time, what type of GUI element to display to collect the answer (Text box, checkboxes, radio buttons, combo boxes, etc) and the answers. There's over 100 questions stashed into these objects and its likely that I will add at least a hundred more before I am through. Big project.
Can you add the answers into that object? Or create an analogous object containing the answers?
Yes the answers can be added into the object. The goal was to be able to go back and forth through all of the questions and be able to recall answers to all of the questions by only passing the object to the class controlling the GUI
Sounds like you need a hierarchical format like xml - a Questionnaire containing Sections containing Questions, each having a type - one_of (radio buttons), many_of (checkboxes), sentence (text input), essay (textbox). Your program then needs to know how to render each question-type. This is useful in several ways - you only have to write the logic once for all qs of a given type, you can use a QuestionFactory object to make it dead simple to add new question-types, you can write multiple interfaces (web page, app, desktop program) to render qs.
Or JSON, I find JSON easier to read/write than XML, personally.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.