I’m new to Python (and also to stackoverflow, as you will easily notice it !)
I’m actually trying to write a program that would work as follow :
the user launch the program.
he’s being asked whether he wants to enter a new word, and the translation of that word.
The word and its translation are stored in a file (data.txt).
When he’s done adding new words, the quiz starts.
The program pick a word, and ask the user for the translation. If the answer is similar to the translation in the file, the program returns “Great !”, if not, it prints the correct answer.
As you can see, it’s pretty simple. My problem here is working with the file, especially retrieving what is inside the file and using it correctly.
Here is my code :
#!/usr/bin/python3.2
# -*-coding:Utf-8 -*
#Vocabulary/translation quiz
import os
import random
keep_adding=input("Would you like to add a new word ? If yes, press \"O\" : ")
while keep_adding=="O":
entry=[]
word=input("Enter a word : ")
word=str(word)
entry.append(word)
translation=input("And its translation : ")
translation=str(translation)
entry.append(translation)
entry=str(entry)
f = open("data.txt","a")
f.write(entry)
f.close()
keep_adding=input("To continue, press \"O\" : ")
f = open("data.txt","a") #in case the file doesn't exist, we create one
f.close()
os.system('clear')
print("* * * QUIZ STARTS ! * * *")
f = open("data.txt","r")
text = f.readlines()
text = list(text)
print("What is the translation for : ",text[0], "?")
answer = input("Answer : ")
if (answer == text[1]):
print("Congratulations ! That's the good answer !")
else:
print("Wrong. The correct answer was : ",text[1])
Thanks a lot for your help !
EDIT : did bring some corrections to my code.
What I get is the following :
* * * QUIZ STARTS ! * * *
What is the translation for : ['alpha', 'bravo']['one', 'two']['x', 'y'] ?
Answer : alpha
Traceback (most recent call last):
File "Python_progs/voc.py", line 43, in <module>
if (answer == text[1]):
IndexError: list index out of range
and in my file, I have this :
['alpha', 'bravo']['one', 'two']['x', 'y']
So actually, I would like to get only the first word in the question (i.e. alpha) and when answering bravo, having it right.
The problem
You’re main issue is the way you’re storing/retrieving things in the quiz file.
You’re doing
f.write(str(entry)), which is writing the string representation of the entry. I’m not quite sure what your intent is here, but you should realize two things: 1)strrepresentations of lists are tricky to turn back into lists (when reading the file) and 2)write()doesn’t append newlines at the end. If you were to do:Then your file would look like this:
Anyway, everything is saved on one line, so when you do
f.readlines(), this returns an object like so:Or more generally:
As you can see, it’s a list with only one item in it. That’s why you get an error when you do
You’re trying to access a second item that doesn’t exist.
The solution?
What you need to do is store each quiz/answer pair as a separate line, with a specific delimiter separating the quiz and answer:
For example:
And to read this file, you’d do something like this: