RoboCop87
RoboCop87

Reputation: 837

How to manage many variables

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?

Upvotes: 0

Views: 523

Answers (2)

OldGeeksGuide
OldGeeksGuide

Reputation: 2918

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.

Upvotes: 0

Hugh Bothwell
Hugh Bothwell

Reputation: 56644

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

Upvotes: 1

Related Questions