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})
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to