I like game shows and trivia questions. I like Python and computers. I thought it would be fun to try to merge the two. I found a list of Family Feud questions from the SNES game of the same name on GameFAQs. I copied it out to a file and tried to parse it through Python so it would give me a random question and its answers.
I started in January but just managed to get it working yesterday. I need to redo it into a dictionary or something with key-value pairs (The question string, and a set of answers). There are also some cases where it doesn’t work.
It’s heavily commented because I hate doing something like this and coming back to it later with no idea how it works.
This might seem simple (and it is) but it’s a big step for me because I figured it out of my own volition and some Googling. I usually just search until I find the code I want, but this was different because I thought it through until I figured out the pieces I needed and looked up those pieces.
import random f = open("C:\\path\\to\\questions.txt" , 'rU') questions =  answers =  ansq =  #most of this could be changed. I don't need that answers list I don't think for line in f: if ".." not in line: #if it's a question (i.e. without leader lines to the point amount, thus not an answer) if line == "\n": pass else: questions.append(line) else: #if it's not a question, then it's an answer (which is a wrong assumption I need to fix) if line == "\n": pass else: answers.append(line) #so add it to the answers list instead #if question matches line #look for the answers below it until the next question # while line not in questions # print line qtoask = random.choice(questions) #pick a random question from the questions list f.seek(0) # go back to beginning of file for line in f: if line == qtoask: #rescan it; if the line matches the random question line = f.next() #go to the next line #ansq.append(line) while ".." in line:#and find all the responses to that question (they all have dotted leader lines) #This if condition will never be true if it's in this while block. Duh. if line == "\n": #if the line is a newline (Enter) pass #skip it else: ansq.append(line) #otherwise, add it to the list of answers to the question line = f.next() #go to the next line and do it again print qtoask for a in ansq: #Beautify. Just print the list of answers so it's easy to read print a + "\n" f.close() #TODO: refactor into lists (or dictionaries?)
Things I’ve learned from this so far:
- You have to manually reset the file (via
- It’s easier to exclude than include in order to get a specific set of data
- I was really happy to find out about