I think my largest problem is I don’t know how to ask the question of what it is exactly that I am looking for.
I stole most the code from a flashcard program from http://www.tuxradar.com/content/code-project-build-flash-card-app and modified it a bit to suit my own needs. However, when I get the answer correct it still says I’ve gotten it wrong.
Here is my code:
#!/usr/bin/env python
import os
import random
import time
file1 = open('/root/first.txt', 'w')
file2 = open('/root/second.txt', 'w')
file1.writelines('1\n2\n3\n4\n5')
file2.writelines('0,2\n1,3\n2,4\n3,5\n4,6')
time.sleep(1)
file1.close
file2.close
time.sleep(1)
file1 = open('/root/first.txt', 'r')
file2 = open('/root/second.txt', 'r')
count = 0
score = 0
tries = int(raw_input('How many tries?'))
start_time = time.time()
f1content = file1.readlines()
f2content = file2.readlines()
try:
while count < tries:
os.system('clear')
wordnum = random.randint(0, len(f1content)-1)
correct = str(f2content[wordnum])
print 'Number:', f1content[wordnum], ''
answer = input('\nSurrounding numbers?')
if answer == correct:
raw_input('\nCorrect! Hit enter...')
score = score + 1
else:
print '\nNope, It\'s', correct
raw_input('Hit enter to try a new one...')
count = count + 1
except SyntaxError:
print 'you must enter a value, starting over'
os.system('./flash.py')
finally:
file1.close
file2.close
os.system('rm /root/first.txt')
os.system('rm /root/second.txt')
stop_time = time.time() - start_time
print '\nIt took you', stop_time / 60, 'minutes to get', score, 'out of', count, 'correct'
I postulate that my problem lies in Line 35 where I define correct as
correct = str(f2content[wordnum])
The reason I think this is because if it gives me 1 and I know that the correct answer is 0,2 and I type that in, it say’s nope, it’s 0,2. This suggests that in plain text it is the exact same to the human eye but that the computer is reading it as something different. I tried to make it a string because of this and making it an integer causes an error. I’m really stuck and am sure it’s something so simple but I am just not seeing it. Any help would be appreciated. Even if it is just a point in the right direction of where I can find the answer.
First, I fixed all the problems given in the comments above.
Instead of
writeline, usewrite:Add the parentheses to
close:Indent everything between the
whileand the end of thetry.Debug by adding the following before the comparison:
Also, I went through and changed all the
/root/first.txt, etc. into justfirst.txt, so it’ll work on a system that doesn’t have a writable directory named/root(which most systems won’t).So, when I run it, I see this:
So clearly,
answeris the tuple(3, 5)rather than the string3,5, andcorrectis the string3,5\nrather than the string3,5.If you read the documentation for input, it should be clear why
answeris wrong. It’s trying to interpret my3,5as a Python expression, and it’s a perfectly valid way to write the tuple(3, 5), so that’s why I get. To fix that, you want to useraw_input.If you read the documentation for readlines, it should be clear why each line has a newline at the end. There are a number of ways around this.
Having fixed that, it still fails at the end, because it never creates the files
table.txtandpocket.txtthat it tries to delete. Whatever you were trying to do there, you appear to have missed something. I just commented out those lines.While you’re at it, I’d change the
open/readlines/closeinto a more modern Python pattern, usingwithand treating the file as just a string iterator. And hardcoding paths like/root/first.txtisn’t a very good idea.Anyway, I don’t want to give the exact changes needed, because this feels like homework, but I think the above is enough information for you to fix it yourself, and hopefully learn a little about how to debug the next problem on your own. If you still have problems, ask away.