Feeds:
Posts

## What I’ve learned in Python this week

• Single quotes and double quotes are the same
• How to check if strings are anagrams of each other (I’m really proud of the way I did this)
• Non-recursive factorials. I cheated a tad, but I almost had what was suggested. I fixed it on my own, though.
• How to convert decimal to binary (not in Python, by hand, but it was in a video series about Python)
• How to use Wing IDE to view a watch window for Python
• What <> means, and that it’s no longer supported

## Adventures in Python file parsing

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 = []
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:

#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

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 seek())
• It’s easier to exclude than include in order to get a specific set of data
• The next() function
• I was really happy to find out about random.choice()

## Pygame physics resources

I’m not a math person, but I’ve been wanting to redo an old project for a little while now. I don’t understand stuff like vectors and deltas. However, I found these two links yesterday and I think they’ll really help me.

If you’re wondering, I want to remake my Pong clone so the ball bounces at angles other than 90 degrees. I’d also like to fix the collision detection (which the second link handles).

## Got a Bluetooth mouse

It’s really fun to use with a jailbroken iPad and BTC Mouse and Trackpad.