[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Ideas about storing questions/answers for a boardGame.



> from os import listdir
>
> from filename in listdir:
>    file = open(filename, "rb")
>    text = file.read()
>    (topic, question, answer) = text.split("--")
>    if not base.has_key(topic):
>       base[topic] = []
>    base[topic] += [(question, answer)]

Sorry, buggy code. Corrections:

from os import listdir

(...)

base = {}

(...)

from filename in listdir(QUESTIONS_DIR):
   file = open(filename, "rb")
   text = file.read()
   (topic, question, answer) = text.split("--")
   if not base.has_key(topic):
      base[topic] = []
   base[topic] += [(question, answer)]
   file.close()

The code abode does not deal with errors in the text files, but it
builds a dictionary with all the topics you have under the variable
"base".

-Thiago