glist wrote:
Let's say I have a file with this kind of content/lines

textdata = '''
%question What is Python?
%correct A programming language
%wrong A graphical package
%wrong An operating system

%question Is Computer Science really about computers?
%wrong Yes
%correct No
'''

I want to pose the question to a user and check his answer, so I come up with this solution. Is there a better
way thought? I particulary hate that "ptr = -1" hack of mine...

questionary = []
ptr = -1

for line in textdata.split('\n'):
    if line:
        space = line.find(' ')
        tag = line[1:space]
        content = line[space+1:]

        if tag == "question":
            ptr += 1
            questionary.append({tag : content})
        else:
questionary[ptr].update({tag : content})
questionary = []

for line in textdata.split('\n'):
   if line:
       tag, content = line.split(' ', 1)
       if tag == "question":
           questionary.append({tag : content})
       else:
           questionary[-1].update({tag : content})

You might even consider making questionary a dictionary keyed by the question.

--
Bob Gailer
919-636-4239 Chapel Hill, NC

_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to